ADODB.Recordset 关闭后保留行.. 经典 ASP

ADODB.Recordset keeping rows after close.. Classic ASP

即使在关闭记录集对象后,我仍然从记录集对象中获取旧记录。

下面是我文件中的代码片段 -

SQL = "Select col1, col2 from [Sheet1$]"

Set ExcelConnection = Server.createobject("ADODB.Connection")
ExcelConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ExcelFile & ";Extended Properties='Excel 5.0;HDR=Yes;IMEX=1';"
SET RS = Server.CreateObject("ADODB.Recordset")
RS.Open SQL, ExcelConnection

strSQL = "insert into tbl1 (col1, col2) values ("

IF NOT RS.EOF THEN
    WHILE NOT RS.eof
        FOR EACH Field IN RS.Fields
            values = values & "'" & Field.value & "',"
        NEXT        
        strSQL = strSQL & trim(left(values,Len(values)-1)) & ")"
        objConn.Execute(strSQL)
        RS.movenext
    WEND
END IF       

RS.Close

现在我想从另一个 sheet 读取并插入到另一个 table -

SQL = "Select col1, col2 from [Sheet2$]"

Set ExcelConnection = Server.createobject("ADODB.Connection")
ExcelConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ExcelFile & ";Extended Properties='Excel 5.0;HDR=Yes;IMEX=1';"    
RS.Open SQL, ExcelConnection

strSQL = "insert into tbl1 (col1, col2) values ("

IF NOT RS.EOF THEN
    WHILE NOT RS.eof
        FOR EACH Field IN RS.Fields
            values = values & "'" & Field.value & "',"
        NEXT        
        strSQL = strSQL & trim(left(values,Len(values)-1)) & ")"
        Response.write(strSQL)      
        objConn.Execute(strSQL) <-- Error here.
        RS.movenext
    WEND
END IF       

RS.Close

以下是我遇到的错误 -

insert into tbl1(col1, col2) values ('1','2','3','4') 

Microsoft OLE DB Provider for SQL Server error '80040e14'

There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement. 

我不知道为什么记录集保留旧值...有什么想法吗?

提前致谢。

您似乎没有清除 "value" 的值。在关闭记录集之前,使用:

value = ""

...以确保它是空的。更好的是,在不同的代码块之间使用不同的变量。