ADODB命令和复杂的SQL命令?

时间:2012-01-09 07:55:02

标签: sql database ms-access vbscript

上下文:Windows7,VBScript,ADODB和ADOX。

我编写了一些VBScript代码,用于创建数据库,创建三个表,使用数据加载其中一个表,然后尝试针对这些表发出一组SQL语句。

除了最后一步之外,一切正常:SQL语句在进入Access自己的查询构建器时工作正常。它们似乎不在Access外部工作,并且没有错误消息。 SQL语句被回显并且显然已执行,但访问数据库内容不会更改。我无法弄明白为什么。

Const dbFile = "C:\database.mdb"
strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Mode=Read|Write;Data Source=" & dbFile
...
Const adVarWChar = 202
Const adLongVarWChar = 203
Const adDouble = 5
Const adInteger = 3

Const adCmdText = 1

Const adLockOptimistic = 3
Const adOpenDynamic = 2
Const adCmdTable = &H0002
Const adUseClient = 3

...

Set objConn = CreateObject("ADODB.Connection")
objConn.CursorLocation = adUseClient
objConn.Open strDSN

Set objCmd  = CreateObject("ADODB.Command")

Set o = createobject("Std.Ini2")
o.Load "C:\Queries.ini"
d = o.GetValue("Setup", "Queries", vbNullString)
a = Split( d, ", ")
For Each s In a
    cmd = o.GetValue(s,"Query",vbNullString)
    WScript.Echo cmd

    With objCmd
        Set .ActiveConnection = objConn
        .CommandText = cmd
        .CommandType = adCmdText
        .CommandTimeout = 60
        .Prepared = True    
        .Execute
    End With
Next

Std.Ini2是我自己的工具之一,可与INI文件进行对话。

INI的示例切片如下:

[Qry-01b-Delete Products from Extg product-import]
Query=DELETE [Product-import].* FROM [Product-import];

[Qry-02-Append Feed To Product-import]
Query=INSERT INTO [Product-import] ( product_sku, product_name, product_price ) SELECT Feed.Col1, Feed.Col2, Feed.Col3 FROM Feed;

[Qry-03a-Delete All Records From Exisiting Category Path Builder]
Query=DELETE [Tbl_Category Path builder].* FROM [Tbl_Category Path builder];

[Qry-03b-Append Products to Category Builder]
Query=INSERT INTO [Tbl_Category Path builder] ( SKU, Product ) SELECT Feed.Col1, Feed.Col2 FROM Feed;

您可能已经解决了这三个表,称为“Feed”,“Product-import”和“Tbl_Category Path builder”。

1 个答案:

答案 0 :(得分:0)

解决方案(对我来说至少)是停止使用ADODB.Command并使用ADODB.Connection的Execute方法,即

Set objConn = CreateObject("ADODB.Connection")
objConn.CursorLocation = adUseClient
objConn.Open strDSN

Set ERS = CreateObject("ADODB.Recordset")

Set o = createobject("Std.Ini2")
o.Load "C:\Users\BruceAxtens\Desktop\Nextra-feed-db-instructions-queries\Nextra_Queries.ini"
d = o.GetValue("Setup", "Queries", vbNullString)
a = Split( d, ", ")
For Each s In a
    cmd = o.GetValue(s,"Query",vbNullString)
    WScript.Echo cmd
    Set ERS = objConn.Execute( cmd )
Next