简体   繁体   English

将数据从SQL导入到Excel

[英]Importing data from SQL to Excel

I'm writing an Application in VB.net and I'm trying to transfer some tables from Sql server to an Excel file. 我正在VB.net中编写应用程序,并且试图将某些表从Sql Server传输到Excel文件。 There are a lot of rows, so I don't want to use loops. 有很多行,所以我不想使用循环。 I tried to adapt code I found: 我尝试调整发现的代码:

 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

What I'm getting is : 我得到的是:

Additional information: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied. 其他信息:[DBNETLIB] [ConnectionOpen(Connect())。] SQL Server不存在或访问被拒绝。

I also would like to add, that I have connected to this database using SqlClient.SqlConnection and I could execute query as normal. 我还想补充一点,我已经使用SqlClient.SqlConnection连接到此数据库,并且可以正常执行查询。

I see two solutions: 我看到两种解决方案:

  • Fix problem with ADODB.Connection, but I'm afraid it will return when I will change database 修复了ADODB.Connection的问题,但是我担心在更改数据库时它将返回
  • Find a way to copy whole table, (without loop) using SqlConnection. 找到一种使用SqlConnection复制整个表(无循环)的方法。

You can use SSIS, if you have it. 您可以使用SSIS(如果有)。 As an alternative, you can consider the following concepts . 或者,您可以考虑以下概念。 . .

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 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://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/

Also . 也。 . .

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

Finally . 最后。 . .

https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction 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