我真的用这个把头发拉了出来。我有一个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是数据库连接)。
问题是我收到错误说:
“有待更改的行数超出限制”
当有多个挂起的更改时,我就明白了。
我想我没有正确设置我的交易,但我有点卡住了。不要以为有人可以指出我的方式的错误?非常感谢。
答案 0 :(得分:9)
为了争论我在交易中使用命令的建议,我写道 这个脚本:
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:它显示鳟鱼:
#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秒(没有任何属性摆弄)对我来说似乎并不坏。
答案 1 :(得分:1)
如果您使用的是OLEDB,则需要根据以下知识库文章将CursorLocation属性设置为adUseClient:http://support.microsoft.com/kb/261297
如果一次只做100k,你也可以考虑以较小批量运行
编辑:是的,adUseClient需要定义为= 3,或者只使用数字3就可以了。