1

我已经编写了将数据插入到 excel 表中的代码。我的代码不会引发任何异常,并且每次 excel 文件的大小都会增加 1KB。但是当我打开工作表时,它没有显示任何数据。

我很困惑,无法弄清楚问题所在。请提前帮助和thanx ...!!!

string strSQL = string.Empty;
excelConn.Open();
strSQL = "INSERT INTO [" + sheetName + "$]  ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11]) VALUES(@value1, @value2, @value3, @value4, @value5,@value6, @value7, @value8, @value9 ,@value10, @value11)";
excelCommand = new OleDbCommand(strSQL, excelConn);
excelCommand.Parameters.AddWithValue("@value1", Program);
excelCommand.Parameters.AddWithValue("@value2", District);
excelCommand.Parameters.AddWithValue("@value3", Period);
excelCommand.Parameters.AddWithValue("@value4", paramValue1);
excelCommand.Parameters.AddWithValue("@value5", paramValue2);
excelCommand.Parameters.AddWithValue("@value6", paramValue3);
excelCommand.Parameters.AddWithValue("@value7", BusinessLogic);
excelCommand.Parameters.AddWithValue("@value8", ExpectedResult);
excelCommand.Parameters.AddWithValue("@value9", ActualResult);
excelCommand.Parameters.AddWithValue("@value10", Status);
excelCommand.Parameters.AddWithValue("@value11", DateTime);               

excelCommand.ExecuteNonQuery();
return true;
4

1 回答 1

3

OLEDB 要求不应命名参数。您应该在查询中使用问号:

strSQL = "INSERT INTO [" + sheetName + "$]  
         ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11]) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
于 2013-02-21T11:55:02.993 回答