错误是无法将对象转换为sql参数
我正在使用bol-> dal,构建一系列参数传递给dal:
BOL:
SqlParameter[] sqlParams = new SqlParameter[]
{
new SqlParameter("@p1", SqlDbType.VarChar, 30).Value = "val1",
new SqlParameter("@p2", SqlDbType.VarChar, 30).Value = "val2"
};
DAL:
public static int ExecuteNonQuery(string sql, SqlParameter[] @params)
{
SqlConnection cnn = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, cnn);
for (int i = 0; i <= @params.Length - 1; i++)
{
cmd.Parameters.Add(@params[i]);
}
foreach (IDataParameter param in cmd.Parameters)
{
if (param.Value == null) param.Value = DBNull.Value;
}
cnn.Open();
int retval = cmd.ExecuteNonQuery();
cnn.Close();
return retval;
}
我需要帮助改进这一点,谢谢 -
答案 0 :(得分:0)
您需要传入实际的SqlParameter。注意Value的对象初始值设定项:
var sqlParams = new SqlParameter[]
{
new SqlParameter("@p1", SqlDbType.VarChar, 30) {Value = "val1"},
new SqlParameter("@p2", SqlDbType.VarChar, 30) {Value = "val2"},
};