2017-10-26 5 views
0

VB.net에서 응용 프로그램을 작성하고 SQL 서버의 일부 테이블을 Excel 파일로 전송하려고합니다. 많은 행이 있으므로 루프를 사용하고 싶지 않습니다.SQL에서 Excel로 데이터 가져 오기

Dim cnPubs As ADODB.Connection 
    cnPubs = New ADODB.Connection 
    Dim strConn As String 
    strConn = "PROVIDER= SQLOLEDB;" 
    strConn = strConn & "DATA SOURCE=(LocalDB)\v11.0;" 
    strConn = strConn & "AttachDbFilename='" & DBPath & "';" 
    strConn = strConn & " INTEGRATED SECURITY=sspi;" 
    cnPubs.Open(strConn) 
    Dim rsPubs As ADODB.Recordset 
    rsPubs = New ADODB.Recordset 

    With rsPubs 
     .ActiveConnection = cnPubs 
     .Open("SELECT * FROM dbo.Table") 
     ExWS.Range("A1").CopyFromRecordset(rsPubs) 
     .Close() 
    End With 
    ExApp.Visible = True 
    cnPubs.Close() 
    rsPubs = Nothing 
    cnPubs = Nothing 

내가 받고 있어요 것은 : 나는 내가 찾은 코드를 적응하기 위해 노력

추가 정보 : [DBNETLIB] [. ConnectionOpen (Connect를())] SQL Server가 없거나 액세스 거절 당했다.

또한이 데이터베이스에 SqlClient.SqlConnection을 사용하여 연결했으며 정상적으로 쿼리를 실행할 수 있다고 덧붙이고 싶습니다. 나는 (루프 제외), 전체 테이블을 복사 할 수있는 방법을 찾기 데이터베이스를

  • 을 바꿀 때 반환됩니다 ADODB.Connection와

    • 수정 문제,하지만 난 두려워 :

      나는이 개 솔루션을 참조 SqlConnection을 사용합니다.

  • +1

    은 내가 도울 수있는 방법을 설정 SQL-Server 관리 Studio에 대한 지침과 함께 작업 코드 예제가 있습니다. https://code.msdn.microsoft.com/Export-Excel-from-SQL-3d994cb5?redir=0 –

    답변

    0

    갖고 계시다면 SSIS를 사용할 수 있습니다. 또는 다음 개념을 고려할 수 있습니다. . .

    Sub ADOExcelSQLServer() 
        ' Carl SQL Server Connection 
        ' 
        ' FOR THIS CODE TO WORK 
        ' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library 
        ' 
    
        Dim Cn As ADODB.Connection 
        Dim Server_Name As String 
        Dim Database_Name As String 
        Dim User_ID As String 
        Dim Password As String 
        Dim SQLStr As String 
        Dim rs As ADODB.Recordset 
        Set rs = New ADODB.Recordset 
    
        Server_Name = "EXCEL-PC\EXCELDEVELOPER" ' Enter your server name here 
        Database_Name = "AdventureWorksLT2012" ' Enter your database name here 
        User_ID = "" ' enter your user ID here 
        Password = "" ' Enter your password here 
        SQLStr = "SELECT * FROM [SalesLT].[Customer]" ' Enter your SQL here 
    
        Set Cn = New ADODB.Connection 
        Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ 
        ";Uid=" & User_ID & ";Pwd=" & Password & ";" 
    
        rs.Open SQLStr, Cn, adOpenStatic 
        ' Dump to spreadsheet 
        With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here 
         .ClearContents 
         .CopyFromRecordset rs 
        End With 
        '   Tidy up 
        rs.Close 
        Set rs = Nothing 
        Cn.Close 
        Set Cn = Nothing 
    End Sub 
    

    OR

    Sub ADOExcelSQLServer() 
    
        Dim Cn As ADODB.Connection 
        Dim Server_Name As String 
        Dim Database_Name As String 
        Dim User_ID As String 
        Dim Password As String 
        Dim SQLStr As String 
        Dim rs As ADODB.Recordset 
        Set rs = New ADODB.Recordset 
    
        Server_Name = "LAPTOP\SQL_EXPRESS" ' Enter your server name here 
        Database_Name = "Northwind" ' Enter your database name here 
        User_ID = "" ' enter your user ID here 
        Password = "" ' Enter your password here 
        SQLStr = "SELECT * FROM Orders" ' Enter your SQL here 
    
        Set Cn = New ADODB.Connection 
        Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ 
        ";Uid=" & User_ID & ";Pwd=" & Password & ";" 
    
        rs.Open SQLStr, Cn, adOpenStatic 
    
        With Worksheets("Sheet1").Range("A2:Z500") 
         .ClearContents 
         .CopyFromRecordset rs 
        End With 
    
        rs.Close 
        Set rs = Nothing 
        Cn.Close 
        Set Cn = Nothing 
    End Sub 
    

    ORRRRRRrrr

    Sub TestMacro() 
    
    ' Create a connection object. 
    Dim cnPubs As ADODB.Connection 
    Set cnPubs = New ADODB.Connection 
    
    ' Provide the connection string. 
    Dim strConn As String 
    
    'Use the SQL Server OLE DB Provider. 
    strConn = "PROVIDER=SQLOLEDB;" 
    
    'Connect to the Pubs database on the local server. 
    strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=NORTHWIND.MDF;" 
    
    'Use an integrated login. 
    strConn = strConn & " INTEGRATED SECURITY=sspi;" 
    
    'Now open the connection. 
    cnPubs.Open strConn 
    
    ' Create a recordset object. 
    Dim rsPubs As ADODB.Recordset 
    Set rsPubs = New ADODB.Recordset 
    
    With rsPubs 
        ' Assign the Connection object. 
        .ActiveConnection = cnPubs 
        ' Extract the required records. 
        .Open "SELECT * FROM Categories" 
        ' Copy the records into cell A1 on Sheet1. 
        Sheet1.Range("A1").CopyFromRecordset rsPubs 
    
        ' Tidy up 
        .Close 
    End With 
    
    cnPubs.Close 
    Set rsPubs = Nothing 
    Set cnPubs = Nothing 
    
    End Sub 
    
    또한

    https://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/

    . . . 마지막으로

    https://www.red-gate.com/simple-talk/sql/t-sql-programming/sql-server-excel-workbench/

    . . .

    https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction

    관련 문제