簡體   English   中英

將數據從SQL導入到Excel

[英]Importing data from SQL to Excel

我正在VB.net中編寫應用程序,並且試圖將某些表從Sql Server傳輸到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復制整個表(無循環)的方法。

您可以使用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

要么

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#簡介

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM