c# - C# 中的 OleDbConnection

标签 c# excel oledb

我已经编写了用于将数据插入到 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;

最佳答案

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

strSQL = "INSERT INTO [" + sheetName + "$]  
         ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11]) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

关于c# - C# 中的 OleDbConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001277/

相关文章:

c# - 解析多种格式的日期时间

excel - 找不到 OleDB 数据提供程序 VBA/Excel

excel - 从列中拆分单词并根据数组中的条件重新加入

excel - 如何将查询导出到 Excel 而不将备忘录截断为 255 个字符?

c# - 使用OLEDB数据提供程序读取excel文件

.net - OleDB 数据库到 SQLite 数据库

c# - 事件处理程序和协方差

c# - 如何编写多行 RegEx 表达式

c# - ASP.Net Web API 不会从 StreamContent 中读取所有字节

excel - 添加对 "No help available"Excel、VBA、用户定义函数的帮助