SqlDataAdapter.DeleteCommand()可以与AddWithValue()一起使用,但无法与Add()一起使用-为什么?

3

微软官方文档提供了一个删除记录的示例:

// Create the DeleteCommand.
command = new SqlCommand(
    "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
    "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

adapter.DeleteCommand = command;

但是在我的系统上不起作用(抱怨缺少@CustomerID)。如果我替换


command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");

以上使用 < /p > 标签。
command.Parameters.AddWithValue("@CustomerID", 5);

它有效

为什么?

1个回答

5
在这一行中:
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");

在这个重载中,5是指参数的长度,而不是值。如果你想要添加值,你需要添加以下内容:
command.Parameters["@CustomerID"].Value = "5";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接