使Excel VBA写入SQL更快。

huangapple go评论133阅读模式
英文:

Make excel VBA write to SQL faster

问题

我有一个经过调整的VBA脚本,用于写入SQL数据库(写入约1500行)。

脚本可以运行,但需要5分钟的时间(时间太长),并导致Excel VBA显示为未响应。

有什么方法可以提高写入SQL的速度?

如何更改此脚本以更快地写入SQL?

  1. Public Sub ConnectToDB()
  2. Dim DBCONT As Object
  3. Dim strConn As String
  4. Dim Database_Name As String
  5. Dim Table_Name As String
  6. Dim strSQL As String
  7. Dim rs As Object
  8. Dim LastRow As Long
  9. Dim i As Long
  10. Dim sCountry, sAsset_type, sYear, sQuarter, sinput_remaining_budget_pct_of_post_ufl_production As String
  11. ' 设置所有变量
  12. Database_Name = "<数据库名称>" ' 在此处输入数据库名称
  13. Table_Name = "<表名称>"
  14. WkbName = ThisWorkbook.Name
  15. SheetName = "mine_ufl_input" ' 在这里定义RS所在的工作表
  16. ' 设置SQL字符串
  17. ' 预备语句
  18. strSQL = "INSERT INTO [ufl]." & Table_Name & _
  19. " ([Country], [Asset_type], [Year], [Quarter],[updated_budget_abs])" & _
  20. " VALUES (?, ?, ?, ?, ?)"
  21. ' 设置连接变量
  22. Set DBCONT = CreateObject("ADODB.Connection")
  23. Set rs = CreateObject("ADODB.Recordset")
  24. DBCONT.ConnectionString = "Provider=sqloledb;Server=<服务器名称>;Database=<数据库名称>;Trusted_connection=yes;"
  25. DBCONT.Open
  26. DBCONT.Execute "truncate table <表名称>"
  27. Set sh = ActiveSheet
  28. LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
  29. Debug.Print LastRow
  30. For i = 2 To LastRow
  31. Set cmd = CreateObject("ADODB.Command")
  32. With cmd
  33. .ActiveConnection = DBCONT ' 连接对象
  34. .CommandText = strSQL ' SQL字符串
  35. .CommandType = adCmdText
  36. ' 绑定参数
  37. .Parameters.Append .CreateParameter("sCountryParam", adVarChar, adParamInput, 255, sh.Cells(i, 1))
  38. .Parameters.Append .CreateParameter("sAsset_typeParam", adVarChar, adParamInput, 255, sh.Cells(i, 2))
  39. .Parameters.Append .CreateParameter("sYearParam", adVarChar, adParamInput, 255, sh.Cells(i, 3))
  40. .Parameters.Append .CreateParameter("sQuarterParam", adVarChar, adParamInput, 255, sh.Cells(i, 4))
  41. .Parameters.Append .CreateParameter("supdated_budget_absParam", adVarChar, adParamInput, 255, sh.Cells(i, 17))
  42. '... 其他参数
  43. .Execute ' 运行操作
  44. End With
  45. Set cmd = Nothing
  46. Next i
  47. Call CloseDB
  48. MsgBox i & "行已导入。"
  49. End Sub

注意:在脚本中,需要将"<数据库名称>"和"<表名称>"替换为实际的数据库名称和表名称。

英文:

I have an adapted VBA script that writes to an SQL database (writes ~1500 rows).

The script works, it takes 5min to run (too long) and causes excel vba to show up as not responding.

Any methods to improve the speed of the write to SQL would be helpful.

How would one change this script to write to SQL faster?

  1. Public Sub ConnectToDB()
  2. Dim DBCONT As Object
  3. Dim strConn As String
  4. Dim Database_Name As String
  5. Dim Table_Name As String
  6. Dim strSQL As String
  7. Dim rs As Object
  8. Dim LastRow As Long
  9. Dim i As Long
  10. Dim sCountry, sAsset_type, sYear, sQuarter, sinput_remaining_budget_pct_of_post_ufl_production As String
  11. &#39; SET ALL VARIABLES
  12. Database_Name = &quot;&lt;DATABASE_NAME&gt;&quot; &#39; Enter your database name here
  13. Table_Name = &quot;&lt;TABLE_NAME&gt;&quot;
  14. WkbName = ThisWorkbook.Name
  15. SheetName = &quot;mine_ufl_input&quot; &#39; WHERE RS IS
  16. &#39; SET SQL STRING
  17. &#39; PREPARED STATEMENT
  18. strSQL = &quot;INSERT INTO [ufl].&quot; &amp; Table_Name &amp; _
  19. &quot; ([Country], [Asset_type], [Year], [Quarter],[updated_budget_abs])&quot; &amp; _
  20. &quot; VALUES (?, ?, ?, ?, ?)&quot;
  21. &#39; SET TO CONNECTION VARIABLES
  22. Set DBCONT = CreateObject(&quot;ADODB.Connection&quot;)
  23. Set rs = CreateObject(&quot;ADODB.Recordset&quot;)
  24. DBCONT.ConnectionString = &quot;Provider=sqloledb;Server=&lt;SERVER_NAME&gt;;Database=&lt;DATABASE_NAME&gt;;Trusted_connection=yes;&quot;
  25. DBCONT.Open
  26. DBCONT.Execute &quot;truncate table &lt;TABLE_NAME&gt;&quot;
  27. Set sh = ActiveSheet
  28. LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row
  29. Debug.Print LastRow
  30. For i = 2 To LastRow
  31. Set cmd = CreateObject(&quot;ADODB.Command&quot;)
  32. With cmd
  33. .ActiveConnection = DBCONT &#39; CONNECTION OBJECT
  34. .CommandText = strSQL &#39; SQL STRING
  35. .CommandType = adCmdText
  36. &#39; BINDING PARAMETERS
  37. .Parameters.Append .CreateParameter(&quot;sCountryParam&quot;, adVarChar, adParamInput, 255, sh.Cells(i, 1))
  38. .Parameters.Append .CreateParameter(&quot;sAsset_typeParam&quot;, adVarChar, adParamInput, 255, sh.Cells(i, 2))
  39. .Parameters.Append .CreateParameter(&quot;sYearParam&quot;, adVarChar, adParamInput, 255, sh.Cells(i, 3))
  40. .Parameters.Append .CreateParameter(&quot;sQuarterParam&quot;, adVarChar, adParamInput, 255, sh.Cells(i, 4))
  41. .Parameters.Append .CreateParameter(&quot;supdated_budget_absParam&quot;, adVarChar, adParamInput, 255, sh.Cells(i, 17))
  42. &#39;... rest of parameters
  43. .Execute &#39; RUN ACTION
  44. End With
  45. Set cmd = Nothing
  46. Next i
  47. Call CloseDB
  48. MsgBox i &amp; &quot; Lines Imported.&quot;
  49. End Sub

答案1

得分: 1

以下是代码部分的翻译:

  1. Takes &lt;1 second on my local server.
英文:

Takes <1 second on my local server.

  1. Option Explicit
  2. Public Sub ConnectToDB()
  3. Dim dbName As String, tableName As String
  4. Dim sConn As String, dbConn As ADODB.Connection, cmd As ADODB.Command
  5. Dim ws As Worksheet, lastrow As Long, r As Long
  6. Dim t0 As Single, p As Long, SQL As String
  7. &#39; SET ALL VARIABLES
  8. dbName = &quot;&lt;DATABASE_NAME&gt;&quot; &#39; Enter your database name here
  9. tableName = &quot;&lt;TABLENAME&gt;&quot;
  10. &#39; SET SQL STRING
  11. SQL = &quot;INSERT INTO [ufl].&quot; &amp; tableName &amp; _
  12. &quot; (Country, Asset_type, Year, Quarter,updated_budget_abs)&quot; &amp; _
  13. &quot; VALUES (?, ?, ?, ?, ?)&quot;
  14. &#39; SET TO CONNECTION VARIABLES
  15. sConn = &quot;Provider=sqloledb;Server=&lt;SERVER_NAME&gt;;&quot; &amp; _
  16. &quot;Database=&quot; &amp; dbName &amp; &quot;;Trusted_connection=yes;&quot;
  17. Set dbConn = New ADODB.Connection
  18. With dbConn
  19. .ConnectionString = sConn
  20. .Open
  21. .Execute &quot;TRUNCATE TABLE &quot; &amp; tableName
  22. End With
  23. &#39; PREPARED STATEMENT
  24. Set cmd = New ADODB.Command
  25. With cmd
  26. .ActiveConnection = dbConn
  27. .CommandText = SQL
  28. .CommandType = adCmdText
  29. .Prepared = True
  30. &#39; BINDING PARAMETERS
  31. For p = 0 To 4
  32. .Parameters.Append .CreateParameter(&quot;P&quot; &amp; p, adVarChar, adParamInput, 255)
  33. Next
  34. End With
  35. Dim arFields: arFields = Array(1, 2, 3, 4, 17) &#39; columns
  36. t0 = Timer
  37. &#39; INSERT RECORDS
  38. Set ws = ActiveSheet &#39; or Thisworkbook.sheets(&quot;&quot;mine_ufl_input&quot;)
  39. With ws
  40. lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
  41. dbConn.Execute &quot;BEGIN TRANSACTION&quot;
  42. For r = 2 To lastrow
  43. For p = 0 To 4
  44. cmd.Parameters(p).Value = .Cells(r, arFields(p))
  45. Next
  46. cmd.Execute &#39; RUN ACTION
  47. Next
  48. dbConn.Execute &quot;COMMIT TRANSACTION&quot;
  49. End With
  50. &#39;Call CloseDB
  51. MsgBox lastrow - 1 &amp; &quot; Lines Imported.&quot;, _
  52. vbInformation, Format(Timer - t0, &quot;0.0 secs&quot;)
  53. End Sub
  54. </details>

huangapple
  • 本文由 发表于 2023年3月12日 17:09:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定