在 VBScript 中使用 ADO 中的事务和参数

发布于 2024-11-19 03:11:08 字数 841 浏览 4 评论 0 原文

我对 ADO、VBScript 和 Access 中的参数和事务有点困惑。基本上,我正在经历一个巨大的循环并将结果写入数据库,因此我需要将其包装在事务中,否则需要很长时间。

我编写了以下脚本,该脚本适用于单个参数(尽管这似乎有点长,所以如果有人知道更短的方法,请大喊)。但是,我无法弄清楚如何将其扩展为两个参数:

objConn.BeginTrans  

  set oParm = CreateObject("ADODB.Parameter")
    oParm.Value = ""
    oParm.Type = 200
    oParm.Direction = 1
    oParm.Size = 100

  Set oCmd = CreateObject("ADODB.Command")
    oCmd.ActiveConnection = objConn
    oCmd.commandText = "INSERT INTO table (field) VALUES (?)"
    oCmd.commandType = 1
    oCmd.Parameters.Append oParm


    'Big loop here that goes through lots of lines.

      oCmd.Execute ,"Field",1

    'Loop 

objConn.CommitTrans

例如,如果我想将其扩展为:

oCmd.commandText = "INSERT INTO table (field1, field2) VALUES (?,?)"

我无法弄清楚我如何处理我的参数。我确信我只是在这里很愚蠢,并没有完全理解它们是如何工作的。

I'm a bit stuck with parameters and transactions in ADO, in VBScript and Access. Basically, I'm working through a massive loop and writing the results to a database, so I need to wrap it in a transaction otherwise it takes ages.

I've written the below script which works for a single parameter, (although this seems a bit of a long way of doing it, so if anyone knows a shorter way, please shout). However I can't work out how to expand this to two parameters:

objConn.BeginTrans  

  set oParm = CreateObject("ADODB.Parameter")
    oParm.Value = ""
    oParm.Type = 200
    oParm.Direction = 1
    oParm.Size = 100

  Set oCmd = CreateObject("ADODB.Command")
    oCmd.ActiveConnection = objConn
    oCmd.commandText = "INSERT INTO table (field) VALUES (?)"
    oCmd.commandType = 1
    oCmd.Parameters.Append oParm


    'Big loop here that goes through lots of lines.

      oCmd.Execute ,"Field",1

    'Loop 

objConn.CommitTrans

For example, if I wanted to expand this to:

oCmd.commandText = "INSERT INTO table (field1, field2) VALUES (?,?)"

I can't figure out what I do with my parameters. I'm sure I'm just being stupid here and not quite following how these work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2024-11-26 03:11:08

我从未尝试过通过 Execute 方法传递参数值,所以我不能完全说出问题所在。我要说的是 文档 指出第二个参数应该是值的数组,所以如果您尝试Array("Field1Val", "Field2Val"),也许会起作用。

我通常做的是给每个参数一个名称,然后您可以在循环中引用它来更改其值。您可以使用任何您喜欢的名称,只要每个参数都有唯一的名称即可。举个例子:

' Sometime before your loop
oParm.Name = "foobar"

' Start loop
    oCmd.Parameters("foobar").Value = "someValue"
    oCmd.Execute , , 1
' End loop

就缩短代码而言,我唯一可以提出的建议是使用 CreateParameter 方法来创建参数。这将允许您在一行上设置所有相关属性。

Set oParm = oCmd.CreateParameter("foobar", 200, 1, 100)

I've never tried passing parameter values through the Execute method, so I can't quite say what's wrong. I will say that the documentation states that the second argument should be an array of values, so maybe if you tried Array("Field1Val", "Field2Val"), that would work.

What I usually do is give each parameter a name, then you can reference it within your loop to change its value. You can use any name you like, as long each parameter has a unique name. As an example:

' Sometime before your loop
oParm.Name = "foobar"

' Start loop
    oCmd.Parameters("foobar").Value = "someValue"
    oCmd.Execute , , 1
' End loop

As far as shortening the code, the only suggestion I can make is using the CreateParameter method to, well, create the parameter. That will allow you to set all the relevant properties on one line.

Set oParm = oCmd.CreateParameter("foobar", 200, 1, 100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文