将 ADODB 记录集转储到 XML,然后返回记录集,然后保存到数据库
我按照以下方式使用 ADODB 记录集的 .Save()
方法创建了一个 XML 文件。
dim res
dim objXML: Set objXML = Server.CreateObject("MSXML2.DOMDocument")
'This returns an ADODB recordset
set res = ExecuteReader("SELECT * from some_table)
With res
Call .Save(objXML, 1)
Call .Close()
End With
Set res = nothing
我们假设上面生成的 XML 然后被保存到一个文件中。
我能够将 XML 读回到记录集中,如下所示:
dim res : set res = Server.CreateObject("ADODB.recordset")
res.open server.mappath("/admin/tbl_some_table.xml")
并且我可以毫无问题地循环记录。
然而,我真正想做的是将 res 中的所有数据保存到完全不同的数据库中的表中。我们可以假设 some_table
已存在于另一个数据库中,并且与我最初查询以生成 XML 的表具有完全相同的结构。
我首先创建一个新记录集并使用 AddNew
将 res
中的所有行添加到新记录集
dim outRes : set outRes = Server.CreateObject("ADODB.recordset")
dim outConn : set outConn = Server.CreateObject("ADODB.Connection")
dim testConnStr : testConnStr = "DRIVER={SQL Server};SERVER=dev-windows\sql2000;UID=myuser;PWD=mypass;DATABASE=Testing"
outConn.open testConnStr
outRes.activeconnection = outConn
outRes.cursortype = adOpenDynamic
outRes.locktype = adLockOptimistic
outRes.source = "product_accessories"
outRes.open
while not res.eof
outRes.addnew
for i=0 to res.fields.count-1
outRes(res.fields(i).name) = res(res.fields(i).name)
next
outRes.movefirst
res.movenext
wend
outRes.updatebatch
但是当我第一次尝试分配 res
中的值时,这会失败代码>res 到outRes
。
用于 ODBC 驱动程序的 Microsoft OLE DB 提供程序错误“80040e21”
多步 OLE DB 操作生成错误。检查每个 OLE DB 状态值(如果有)。没有完成任何工作。
有人可以告诉我我做错了什么,或者建议我更好的方法将从 XML 加载的数据复制到其他数据库吗?
更新,部分解决
所以事实证明我的错误是由于我尝试设置 Identity 字段的值引起的。如果我暂时将该字段更改为不是身份,则所有数据都会完美插入。
现在是一个后续问题:
我如何暂时关闭该字段的 Identity 属性,然后在完成更新后将其重新打开?
I've created an XML file using the .Save()
method of an ADODB recordset in the following manner.
dim res
dim objXML: Set objXML = Server.CreateObject("MSXML2.DOMDocument")
'This returns an ADODB recordset
set res = ExecuteReader("SELECT * from some_table)
With res
Call .Save(objXML, 1)
Call .Close()
End With
Set res = nothing
Let's assume that the XML generated above then gets saved to a file.
I'm able to read the XML back into a recordset like this:
dim res : set res = Server.CreateObject("ADODB.recordset")
res.open server.mappath("/admin/tbl_some_table.xml")
And I can loop over the records without any problem.
However what I really want to do is save all of the data in res
to a table in a completely different database. We can assume that some_table
already exists in this other database and has the exact same structure as the table I originally queried to make the XML.
I started by creating a new recordset and using AddNew
to add all of the rows from res
to the new recordset
dim outRes : set outRes = Server.CreateObject("ADODB.recordset")
dim outConn : set outConn = Server.CreateObject("ADODB.Connection")
dim testConnStr : testConnStr = "DRIVER={SQL Server};SERVER=dev-windows\sql2000;UID=myuser;PWD=mypass;DATABASE=Testing"
outConn.open testConnStr
outRes.activeconnection = outConn
outRes.cursortype = adOpenDynamic
outRes.locktype = adLockOptimistic
outRes.source = "product_accessories"
outRes.open
while not res.eof
outRes.addnew
for i=0 to res.fields.count-1
outRes(res.fields(i).name) = res(res.fields(i).name)
next
outRes.movefirst
res.movenext
wend
outRes.updatebatch
But this bombs the first time I try to assign the value from res
to outRes
.
Microsoft OLE DB Provider for ODBC Drivers error '80040e21'
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.
Can someone tell me what I'm doing wrong or suggest a better way for me to copy the data loaded from XML to a different database?
Update, partly solved
So it turns out that my error is caused by my attempting to set the value of an Identity field. If I temporarily change that field to not be an Identity, all of the data gets inserted perfectly.
Now a followup question
How can I temporarily turn off the Identity property of that field, then turn it back on when I'm done with my updates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于上述问题,我永远无法让 Recordset.AddNew 工作。
作为解决方法,我正在执行
SET IDENTITY_INSERT table ON
,执行 INSERT sql,然后SET IDENTITY_INSERT table OFF
。I was never able to get Recordset.AddNew to work because of the above problem.
As a workaround, I'm doing a
SET IDENTITY_INSERT table ON
, executing the INSERT sql, andSET IDENTITY_INSERT table OFF
.读回 XML 后,将记录集的连接属性设置为新的数据库连接,然后调用 UpdateBatch。
After reading the XML back, set the connection property of the recordset to the new database connection and then invoke UpdateBatch.