Reputation: 1179
Microsoft's own documentation provides an example of deleting a record as follows:
// 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;
But on my system it doesn't work (complaining about missing @CustomerID). Instead, if I replace the
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
above with
command.Parameters.AddWithValue("@CustomerID", 5);
Why?
Upvotes: 3
Views: 1709
Reputation: 21630
In this line:
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
In this overload, 5 refers to the length of the parameter, not the value. If you want to add the value, you'll have to add the following:
command.Parameters["@CustomerID"].Value = "5";
Upvotes: 5