Sql 在VBScript中从Excel工作表运行多个查询

Sql 在VBScript中从Excel工作表运行多个查询,sql,excel,vbscript,odbc,sap-ase,Sql,Excel,Vbscript,Odbc,Sap Ase,来自世界各地的脚本编写人员您好,我目前正在使用VBScript从excel工作表中提取查询参数,并在SQL server(Sybase ASE)上为excel工作表中的每一行运行查询 目前,我有成功提取参数并运行第一行的代码,但当我开始下一行时,由于“缺少对象”,查询失败,但我已经尝试保持ADO连接打开,并在记录集中重新运行查询,以及为excel工作表中的每一行重新创建连接并运行记录集。我目前的代码如下: Dim MyConnection,MyRecordSet,MyExcel,MyExcelB

来自世界各地的脚本编写人员您好,我目前正在使用VBScript从excel工作表中提取查询参数,并在SQL server(Sybase ASE)上为excel工作表中的每一行运行查询

目前,我有成功提取参数并运行第一行的代码,但当我开始下一行时,由于“缺少对象”,查询失败,但我已经尝试保持ADO连接打开,并在记录集中重新运行查询,以及为excel工作表中的每一行重新创建连接并运行记录集。我目前的代码如下:

Dim MyConnection,MyRecordSet,MyExcel,MyExcelBook,MyExcelSheet
Dim连接字符串,参数1,参数2,SQL,I
ConnectionString=“Driver=ODBC驱动程序;UserID=Admin;Password=Pass;”
设置MyExcel=CreateObject(“Excel.Application”)
设置MyExcelBook=MyExcel.Workbooks.Open(“C:/Test.xls”)
设置MyExcelSheet=MyExcelBook.WorkSheets(2)
设置MyConnection=CreateObject(“ADODB.Connection”)
Set MyRecordSet=CreateObject(“ADODB.RecordSet”)
MyConnection.开放连接字符串
I=2
在MyExcelSheet.Cells(I,2.Value)时执行此操作“”
参数1=MyExcelSheet.Cells(I,6).Value
参数2=MyExcelSheet.Cells(I,7).Value
SQL=“从Type=“&Parameter1&”和Color=“&Parameter2&;”的表格中选择*”
MyRecordSet.Open SQL,MyConnection
“将记录集保存到其他Excel工作表”
我的记录集。关闭
I=I+1
环
我的连接,关闭
设置MyConnection=Nothing
是否有任何方法可以在与数据库的同一连接上运行另一个查询,而不必运行到

所需对象

错误


编辑后,仍然有相同的错误

Dim MyConnection,MyExcel,MyExcelBook,MyExcelSheet
Dim连接字符串,参数1,参数2,SQL,I
ConnectionString=“Driver=ODBC驱动程序;UserID=Admin;Password=Pass;”
设置MyExcel=CreateObject(“Excel.Application”)
设置MyExcelBook=MyExcel.Workbooks.Open(“C:/Test.xls”)
设置MyExcelSheet=MyExcelBook.WorkSheets(2)
设置MyConnection=CreateObject(“ADODB.Connection”)
MyConnection.开放连接字符串
I=2
在MyExcelSheet.Cells(I,2.Value)时执行此操作“”
参数1=MyExcelSheet.Cells(I,6).Value
参数2=MyExcelSheet.Cells(I,7).Value
SQL=“从Type=“&Parameter1&”和Color=“&Parameter2&;”的表格中选择*”
Dim MyRecordSet
Set MyRecordSet=CreateObject(“ADODB.RecordSet”)
MyRecordSet.Open SQL,MyConnection
“将记录集保存到其他Excel工作表”
我的记录集。关闭
I=I+1
环
我的连接,关闭
设置MyConnection=Nothing

不需要为每个查询打开和关闭连接对象。试着这样做:

Dim MyConnection, MyExcel, MyExcelBook, MyExcelSheet
Dim ConnectionString, Parameter1, Parameter2, SQL, I
ConnectionString = "Driver=ODBC Driver;UserID=Admin;Password=Pass;"

Set MyExcel = CreateObject("Excel.Application")
Set MyExcelBook = MyExcel.Workbooks.Open("C:/Test.xls")
Set MyExcelSheet = MyExcelBook.WorkSheets(2)
I = 2

Set MyConnection = CreateObject("ADODB.Connection")
MyConnection.Open ConnectionString
Do while MyExcelSheet.Cells(I,2) <> ""
    Parameter1 = MyExcelSheet.Cells(I,6).Value
    Parameter2 = MyExcelSheet.Cells(I,7).Value
    SQL = "Select * From Table Where Type=" & Parameter1 & " AND Color=" & Parameter2 & ";"
    Dim MyRecordSet
    Set MyRecordSet = CreateObject("ADODB.RecordSet")
    MyRecordSet.Open SQL, MyConnection
    'Save RecordSet to another Excel sheet'
    MyRecordSet.Close
    Set MyRecordSet = Nothing
    I = I + 1
Loop
MyConnection.Close
Set MyConnection = Nothing
Dim MyConnection,MyExcel,MyExcelBook,MyExcelSheet
Dim连接字符串,参数1,参数2,SQL,I
ConnectionString=“Driver=ODBC驱动程序;UserID=Admin;Password=Pass;”
设置MyExcel=CreateObject(“Excel.Application”)
设置MyExcelBook=MyExcel.Workbooks.Open(“C:/Test.xls”)
设置MyExcelSheet=MyExcelBook.WorkSheets(2)
I=2
设置MyConnection=CreateObject(“ADODB.Connection”)
MyConnection.开放连接字符串
当MyExcelSheet.Cells(I,2)”时执行此操作
参数1=MyExcelSheet.Cells(I,6).Value
参数2=MyExcelSheet.Cells(I,7).Value
SQL=“从Type=“&Parameter1&”和Color=“&Parameter2&;”的表格中选择*”
Dim MyRecordSet
Set MyRecordSet=CreateObject(“ADODB.RecordSet”)
MyRecordSet.Open SQL,MyConnection
“将记录集保存到其他Excel工作表”
我的记录集。关闭
Set MyRecordSet=Nothing
I=I+1
环
我的连接,关闭
设置MyConnection=Nothing

所以在对不同想法进行了大量测试之后,我终于让代码开始工作了。问题发生在MyRecordSet关闭时。状态返回到0,当您尝试重新打开MyRecordSet时,它将因对象损坏而失败。重新连接然后重新打开MyRecordSet会导致相同的错误。下面是完成的代码编辑

Dim MyConnection, MyRecordSet, MyExcel, MyExcelBook, MyExcelSheet
Dim ConnectionString, Parameter1, Parameter2, SQL, I
ConnectionString = "Driver=Adaptive Server Enterprise;UserID=Admin;Password=Pass;Pooling=False"

Set MyExcel = CreateObject("Excel.Application")
Set MyExcelBook = MyExcel.Workbooks.Open("C:/Test.xls")
Set MyExcelSheet = MyExcelBook.WorkSheets(2)
I = 2

Do while MyExcelSheet.Cells(I,2).Value <> ""
    Parameter1 = MyExcelSheet.Cells(I,6).Value
    Parameter2 = MyExcelSheet.Cells(I,7).Value
    SQL = "Select * From Table Where Type=" & Parameter1 & " AND Color=" & Parameter2 & ";"
    Set MyConnection = CreateObject("ADODB.Connection")
    MyConnection.Open ConnectionString
    Set MyRecordSet = CreateObject("ADODB.RecordSet")
    MyRecordSet.Open SQL, MyConnection
    'Save RecordSet to another Excel sheet'
    Set MyRecordSet = Nothing
    Set MyConnection = Nothing
    I = I + 1
loop
Dim MyConnection,MyRecordSet,MyExcel,MyExcelBook,MyExcelSheet
Dim连接字符串,参数1,参数2,SQL,I
ConnectionString=“Driver=adaptiveserverenterprise;UserID=Admin;Password=Pass;Pooling=False”
设置MyExcel=CreateObject(“Excel.Application”)
设置MyExcelBook=MyExcel.Workbooks.Open(“C:/Test.xls”)
设置MyExcelSheet=MyExcelBook.WorkSheets(2)
I=2
在MyExcelSheet.Cells(I,2.Value)时执行此操作“”
参数1=MyExcelSheet.Cells(I,6).Value
参数2=MyExcelSheet.Cells(I,7).Value
SQL=“从Type=“&Parameter1&”和Color=“&Parameter2&;”的表格中选择*”
设置MyConnection=CreateObject(“ADODB.Connection”)
MyConnection.开放连接字符串
Set MyRecordSet=CreateObject(“ADODB.RecordSet”)
MyRecordSet.Open SQL,MyConnection
“将记录集保存到其他Excel工作表”
Set MyRecordSet=Nothing
设置MyConnection=Nothing
I=I+1
环

现在如果你问我它的数据库是否特定,我不知道。但这段代码与AdaptiveServerEnterprise驱动程序配合使用,我确实使用了LankyMart的建议关闭了连接池。我知道只设置“object=nothing”而不首先关闭连接违背了标准的编码实践,但它是有效的。感谢那些帮助过我的人。

我刚刚试着运行它,但它仍然会遇到“需要对象”运行时错误。连接仍处于打开状态,但在重新创建记录集对象时失败请尝试上述修订,我将
Dim MyRecordSet
放在循环内部。然后我遇到了“Object Redefined”编译错误。我会在前面编辑我的评论,说明它是在使用MyConnection.open而不是创建对象时生成的。您是否从顶部删除了
Dim MyRecordSet
?另外,
MyConnection
不应该在循环中。那么错误是在循环之前发生的?很抱歉,在同一时间处理另一个项目。但是