gpt4 book ai didi

c# - OleDb 命令,更新

转载 作者:行者123 更新时间:2023-11-30 19:39:30 26 4
gpt4 key购买 nike

具有一个访问表 (CustomersTable) 和两个字段(CustomerID、CustomerName):

command.CommandText = "UPDATE CustomersTable   SET CompanyName =  @p2   WHERE CompanyName =  @p1";
command.Parameters.Clear();
command.Parameters.Add("@p1", OleDbType.VarChar).Value = "xyz3";
command.Parameters.Add("@p2", OleDbType.VarChar).Value = "xyz4";
command.ExecuteNonQuery();

不更改公司名称字段。

最佳答案

OleDbCommand 不支持命名参数。

来自 OleDbCommand.Parameters property

The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ?

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

实际上,? 不是必须的,但它是一个comman。

也可以使用 using statement处理您的数据库连接和对象。

using(OleDbConnection con = new OleDbConnection(conString))
using(OleDbCommand command = con.CreateCommand())
{
command.CommandText = "UPDATE CustomersTable SET CompanyName = ? WHERE CompanyName = ?";
command.Parameters.Add("@p1", OleDbType.VarChar).Value = "xyz3";
command.Parameters.Add("@p2", OleDbType.VarChar).Value = "xyz4";
con.Open();
command.ExecuteNonQuery();
}

关于c# - OleDb 命令,更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27453503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com