将数据从SQL Server加载到Excel的最快方法

时间:2018-06-03 19:58:39

标签: excel vba excel-vba

我在SQL Server上有一个相对较大的表(大约300,000行)。此数据用作数据池,以验证用户在Excel电子表格中执行的操作。

确保用户始终使用最新数据的一种方法是设置VBA,以便在电子表格打开时自动从SQL Server提取数据。

代码:

Private Sub Workbook_Open()
        Set objMyConn = New ADODB.Connection
        Set objMyRecordset = New ADODB.Recordset
        Dim strSQL As String

    'Open Connection'
        objMyConn.ConnectionString = "Provider=SQLOLEDB; Data Source=Server Name;Initial Catalog=Database;User ID=User;Password=Password; Trusted_Connection=no"
        objMyConn.Open

    'Set and Excecute SQL Command'
        strSQL = "SELECT * FROM [Database]"

    'Open Recordset'
        Set objMyRecordset.ActiveConnection = objMyConn
        objMyRecordset.Open strSQL

    'Copy Data to Excel'
        Sheets("TEPSD").Range("A1").CopyFromRecordset (objMyRecordset)

        objMyConn.Close

End Sub

这个问题是需要很长时间。有时,它需要太长时间,我最终不得不按住它来打破它。

我想知道是否有更快的方法这样做?或者另一种验证用户数据的方法,而不必将整个表拉到Excel?

非常感谢任何意见。

2 个答案:

答案 0 :(得分:3)

在我们的讨论之后。

目前,每个用户都试图在很短的时间内从服务器中提取大量数据,然后数据将变得陈旧,因为除非用户关闭并重新打开工作簿,否则数据不会更新。

而是切换到Worksheet_Change()宏。这将在每次更改时刷新数据。

这将允许您在每次用户进行更改时发送自定义查询 - 例如按下进入。

将您的strSQL从静态查询中更改为将所有数据抽取到strSQL = "SELECT name FROM [Database] WHERE name = " & Target.Value

N.B。您应该查看VBA记录集如何要求您进行参数连接以避免Little Bobbie Tables

如果查询成功 - 即有效条目 - 那么它应该返回相同的名称。如果没有相应的条目 - 即不是有效的选择 - 那么你应该得到一个空的recordset。这意味着您可以使用If rsObj.RecordCount < 1 Then测试查询结果,以便在没有有效匹配时正常退出(或提示用户等)。

答案 1 :(得分:0)

您应该考虑这样的事情。

将Excel连接到SQL Server

用于SQL Server的DEVART Excel加载项允许您将Excel连接到SQL Server数据库,检索实时SQL Server数据并将其加载到Excel,然后修改这些数据并将更改保存回SQL Server。您可以通过以下几个简单的步骤,将Excel连接到SQL Server并将SQL Server数据加载到Excel。

要开始将Excel链接到SQL Server,请在功能区上,单击“ DEVART”选项卡,然后单击“获取数据”按钮。这将显示“导入数据”向导,您需要在其中创建Excel SQL Server连接并配置查询以将数据从SQL Server获取到Excel: Devart功能区选项卡

  1. 指定连接参数

在“连接编辑器”对话框中,您需要输入必要的连接参数:

Login details - select whether to use Windows authentication or SQL Server authentication. In the latter case you need to enter your SQL Server user name and password.
User name - your SQL Server user name. Required only for SQL Server authentication.
Password - your SQL Server password. Required only for SQL Server authentication.
Database - the name of SQL database to connect to Excel.

如果需要更详细地配置Excel SQL Server连接,可以选择单击“高级”按钮并配置高级连接参数。您可以在其中配置连接加密,弹性参数等。

要检查是否已正确将Excel连接到SQL Server,请单击“测试连接”按钮。

Excel SQL连接设置 2.选择是否在Excel工作簿中存储连接

您可以选择更改设置,以在Excel工作簿和Excel设置中存储连接和查询数据的方式:

Allow saving add-in specific data in Excel worksheet - clear this check box in case you don't want to save any Excel add-in specific data in the Excel worksheet - connections, queries, etc. In this case, if you want to reload data from SQL Server to Excel or save modified data back to SQL Server, you will need to reenter both the connection settings and query.
Allow saving connection string in Excel worksheet - clear this check box if you want your SQL Server connection parameters not to be stored in the Excel. In this case you will need to reenter your connection settings each time you want to reload SQL Server data or modify and save them to SQL Server. However, you may share the Excel workbook, and nobody will be able to get any connection details from it.
Allow saving password - it is recommended to clear this check box. If you don't clear this check box, all the connection settings, including your SQL Server password, will be stored in the Excel workbook. And anyone having our Excel Add-in for SQL Server and the workbook will be able to link Excel to the SQL Server, get data from it, and modify them. But in this case you won't need to reenter anything when reloading data from SQL Server to Excel or saving them to SQL Server.
Allow reuse connection in Excel - select this check box if you want to save this connection on your computer and reuse it in other Excel workbooks. It does not affect saving connection parameters in the workbook itself. You need to specify the connection name, and after this you will be able to simply select this connection from the list
  1. 配置查询以获取数据

您可以使用Visual Query Builder对其进行直观配置,或者切换到“ SQL查询”选项卡并键入SQL查询。要直观地配置查询,请执行以下操作:

In the Object list select the SQL Server table to load its data to Excel.

In the tree below clear check boxes for the columns you don't want to import data from.

Optionally expand the relation node and select check boxes for the columns from the tables referenced by the current table's foreign keys to add them to the query.

In the box on the right you may optionally configure the filter conditions and ordering of the imported data and specify the max number of rows to load from SQL Server to Excel. For more information on configuring the query you may refer to our documentation, installed with the Excel Add-ins.

在指定查询后,您可以选择单击“下一步”并预览一些返回的第一行。或单击完成并开始数据加载。 查询以将SQL Server数据检索到Excel

编辑实时SQL Server数据

将数据从SQL Server加载到Excel之后,您可以像处理通常的Excel工作表一样使用这些数据。通过单击功能区的“ Devart”选项卡上的“刷新”,可以立即从SQL Server刷新数据,因此,始终在工作簿中保留来自SQL Server的实时数据。

如果要在Excel中编辑SQL Server数据并将其保存回SQL Server,则需要首先单击功能区的Devart选项卡上的“编辑模式”。否则,您所做的更改将无法保存到SQL Server。

启动“编辑”模式后,您可以像通常在excel中一样编辑数据-删除行,修改其单元格值。无法在SQL Server中编辑的列将具有斜体字体,并且您无法编辑这些列中的值。要添加新行,请在以绿色突出显示的表的最后一行中输入所需的值。 在Excel中编辑SQL Server数据

要将更改应用于数据库中的实际数据,请单击“提交”按钮。或单击“回滚”以回滚所有更改。请注意,即使您保存工作簿,更改也不会保存到数据库,直到您单击“提交”为止。

也请考虑一下。

https://www.mssqltips.com/sqlservertip/3436/passing-dynamic-query-values-from-excel-to-sql-server/

我在办公室中使用这些技术,并且从SQL Server到Excel,我正在接近900k行,大约24列。至少需要60秒才能完成刷新。