首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向OleDbDataWriter添加参数

如何向OleDbDataWriter添加参数
EN

Stack Overflow用户
提问于 2011-12-05 11:45:45
回答 2查看 581关注 0票数 0

当我试图使用ADO.NET写入excel文件时,会出现语法错误。如何向查询中添加参数。我正在更新mysql数据库。

代码语言:javascript
复制
string error="Text for status";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";

System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection(connectionString);
string ExcelQuery;

ExcelQuery = "Update [Sheet1$] set Status="+error; // from Sheet1";

//Create the command to be executed
ExcelCommand = new System.Data.OleDb.OleDbCommand(ExcelQuery, ExcelConnection);

//Open the connection to the file
ExcelConnection.Open();

//Execute the update
ExcelCommand.ExecuteNonQuery();

//Close the connection
ExcelConnection.Close();

查询表达式“状态的文本”中的语法错误(缺少运算符)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-05 11:52:05

可以使用OleDbCommand.Parameters.Add向命令添加参数。

代码语言:javascript
复制
public void CreateMyOleDbCommand(OleDbConnection connection,
    string queryString, OleDbParameter[] parameters) 
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
    command.CommandText = 
        "SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?";
    command.Parameters.Add(parameters);

    for (int j=0; j<parameters.Length; j++)
    {
        command.Parameters.Add(parameters[j]) ;
    }

    string message = "";
    for (int i = 0; i < command.Parameters.Count; i++) 
    {
        message += command.Parameters[i].ToString() + "\n";
    }
    MessageBox.Show(message);
}
票数 0
EN

Stack Overflow用户

发布于 2011-12-05 11:48:50

看起来您在'语句中丢失了UPDATE

ExcelQuery = "Update [Sheet1$] set [Status]='" + error + "'";

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8384791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档