【问题标题】:Fastest way to load data from SQL Server to Excel将数据从 SQL Server 加载到 Excel 的最快方法
【发布时间】:2018-06-03 19:58:38
【问题描述】:

我在 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

问题是它需要很长时间。有时,它花费的时间太长,我最终不得不 ctrl+break 它。

我想知道是否有更快的方法来做到这一点?还是另一种无需将整个表格拉到 Excel 即可验证用户数据的方法?

非常感谢任何意见。

【问题讨论】:

  • 多少列?
  • (1) “最新” - 基线数据多久更改一次? (2) 真的需要所有数据还是可以使用子集? (3) 最后 - 是否所有用户都试图同时访问服务器(即每个人早上 9 点出现并抓住它)
  • 7 列和超过 300,000 行。
  • @Alan 基线数据可以随时更改,恕不另行通知。并非所有数据都是必需的,但我不确定如何验证用户将输入的内容,而无需将所有数据都放在后台某处。最后,用户将随时使用它,将有机会让每个人都同时使用它。

标签: vba excel


【解决方案1】:

在我们的讨论之后。

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

改为使用 Worksheet_Change() 宏。这将在每次进行更改时刷新数据。

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

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

注意您应该真正查看 VBA 记录集希望您如何进行参数连接以避免Little Bobbie Tables

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

【讨论】:

    【解决方案2】:

    你应该考虑这样的事情。

    将 Excel 连接到 SQL Server

    Devart Excel Add-in for SQL Server 允许您将 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 Query 选项卡并键入 SQL Query。要直观地配置查询,请执行以下操作:

    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/

    我在我的办公室使用这些技术,我正在将近 900k 行和大约 24 列从 SQL Server 移动到 Excel。刷新时间不到 60 秒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2012-07-29
      • 2011-05-31
      相关资源
      最近更新 更多