gpt4 book ai didi

ms-access - 使用 Vbscript 将记录批量插入 Access

转载 作者:行者123 更新时间:2023-12-03 08:14:42 24 4
gpt4 key购买 nike

我真的用这个把头发拉出来了。我有一个 vbscript,我正在尝试将几十万条记录插入到 Access 数据库中。

显然,如果我一次做一个真的很慢,所以我想我可以用某种事务批量插入它们。所以我试着写这个:

set rs = CreateObject("ADODB.recordset")
rs.Open "table", objConn,, 4

For counter = 1 to 100000
rs.AddNew
rs("username") = "Value"
Next

rs.UpdateBatch

(objConn 是数据库连接)。

问题是我收到一条错误消息:

"Number of rows with pending changes exceeded the limit"



当有多个待处理的更改时,我就会明白这一点。

我想我没有正确设置我的交易,但我有点卡住了。不要以为有人可以指出我方法的错误吗?非常感谢。

最佳答案

为了支持我在交易中使用命令的提议,我写道
这个脚本:

  Dim sAct      : sAct      = "trout"
If goWAN.Exists( "a" ) Then sAct = goWAN( "a" )
Dim nRecs : nRecs = 10
If goWAN.Exists( "n" ) Then nRecs = CLng( goWAN( "n" ) )
Dim sMFSpec : sMFSpec = goFS.GetAbsolutePathName( "..\data\ut.mdb" )
Dim oConn : Set oConn = CreateObject( "ADODB.Connection" )
Dim oRs : Set oRs = CreateObject( "ADODB.Recordset" )

Dim nRec, oCmd, nRA, aData, oParm

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sMFSpec
Set oRs.ActiveConnection = oConn

oConn.Execute( "DELETE FROM tLines" )
WScript.Echo "#Recs:", oConn.Execute( "SELECT COUNT(SampleText) FROM tLines" ).Fields( 0 )

WScript.Echo sAct
Select Case sAct
Case "trout"
Case "bob"
oRs.CursorLocation = adUseClient
oRs.CursorType = adOpenKeySet
oRs.LockType = adLockBatchOptimistic
Case "eh"
End Select
WScript.Echo "oRs.CursorLocation: ", oRs.CursorLocation
WScript.Echo "oRs.CursorType: ", oRs.CursorType
WScript.Echo "oRs.LockType: ", oRs.LockType
Select Case sAct
Case "trout", "bob"
oRs.Open "tLines", oConn, , adLockBatchOptimistic
For nRec = 1 to nRecs
oRs.AddNew
oRs( "SampleText" ) = "This is line " & nRec
Next
oRs.UpdateBatch
oRs.Close
Case "eh"
oConn.BeginTrans
Set oParm = CreateObject( "ADODB.Parameter" )
With oParm
.Name = "A"
.Type = adVarChar
.Value = ""
.Direction = adParamInput
.Size = 100
End With
Set oCmd = CreateObject( "ADODB.Command" )
With oCmd
Set .ActiveConnection = oConn
.CommandText = "INSERT INTO tLines (SampleText) VALUES (?)"
.CommandType = adCmdText
.Parameters.Append oParm
End With

ReDim aData( 0 )
For nRec = 1 to nRecs
aData( 0 ) = "This is line " & nRec
oCmd.Execute nRA, aData, adExecuteNoRecords + adCmdText
Next
oConn.CommitTrans
End Select

WScript.Echo "#Recs:", oConn.Execute( "SELECT COUNT(SampleText) FROM tLines" ).Fields( 0 )
WScript.Echo "First:", oConn.Execute( "SELECT TOP 1 * FROM tLines" ).Fields( 0 )

oConn.Close

用/n:200 和/a:trout 调用它显示:
  #Recs: 0
trout
oRs.CursorLocation: 2
oRs.CursorType: 0
oRs.LockType: 1
... xpl.vbs(246, 11) Provider: Number of rows with pending changes exceeded the limit.

所以我想,我正确地重现了你的问题。对于/a:bob:
  #Recs: 0
bob
oRs.CursorLocation: 3
oRs.CursorType: 1
oRs.LockType: 4
#Recs: 200
First: This is line 1
xpl.vbs: Erfolgreich beendet. (0) [ 19.74219 secs ]

所以设置
  oRs.CursorLocation = adUseClient
oRs.CursorType = adOpenKeySet
oRs.LockType = adLockBatchOptimistic

正如鲍勃(和微软)所建议的那样,这是解决您问题的一种方法。为了加快速度,我放了一个
命令进入交易:
  oConn.BeginTrans
Set oCmd = CreateObject( "ADODB.Command" )
...
ReDim aData( 0 )
For nRec = 1 to nRecs
aData( 0 ) = "This is line " & nRec
oCmd.Execute nRA, aData, adExecuteNoRecords + adCmdText
Next
oConn.CommitTrans

结果:
#Recs: 0
eh
oRs.CursorLocation: 2
oRs.CursorType: 0
oRs.LockType: 1
#Recs: 200
First: This is line 1
xpl.vbs: Erfolgreich beendet. (0) [ 1.47656 secs ]

从 20 秒到 2 秒(没有任何属性摆弄)对我来说似乎不错。

关于ms-access - 使用 Vbscript 将记录批量插入 Access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6574462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com