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

时间:2015-06-24 15:00:24

标签: asp-classic adodb recordset

即使我关闭了记录集对象,我也从记录集对象中获取旧记录。

以下是我文件中的代码段 -

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

现在我想从另一张纸上读取并插入另一张表 -

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. 

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

您似乎没有清除&#34;值&#34;的价值。在关闭记录集之前,请使用:

value = ""

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