Stored procedures 在执行command.Prepare()时,我有;Prepare方法要求所有参数都有一个显式设置的类型";错误

Stored procedures 在执行command.Prepare()时,我有;Prepare方法要求所有参数都有一个显式设置的类型";错误,stored-procedures,prepared-statement,Stored Procedures,Prepared Statement,我有以下声明: SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["DataBaseName"]); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "update Table1 set data = @data where id = @id"; cmd.Parameters.AddWithValue("

我有以下声明:

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["DataBaseName"]);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "update Table1 set data = @data where id = @id";
cmd.Parameters.AddWithValue("@data", SqlDbType.VarChar).Value =  data;
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value =  id;
cmd.CommandType = CommandType.Text;

try
{
    DataSet ds = new DataSet();
    con.Open();
    cmd.Prepare();
    cmd.ExecuteNonQuery();
    return true;
}
执行
cmd.Prepare()
时,我有一个错误
SqlCommand.Prepare方法要求所有参数都有一个显式设置的类型

我在这里读了一些答案,但看起来我是按照描述做的 但还是有同样的问题


我错过了什么

AddWithValue
不使用类型进行添加。它使用第二个参数作为值。现在有两个非类型化参数,其值是等于枚举成员
SqlDbType.VarChar
SqlDbType.Int
的整数。另外,你可能会喜欢阅读@marc_的打字错误。将
AddWithValue
替换为
Add
,并提供varchar的长度。如果参数是
SqlDbType.DateTime
Int
,则不提供长度。