参数化查询 需要参数 但未提供该参数。
SqlConnectioncon=newSqlConnection("server=(local);IntegratedSecurity=True;database=va...
SqlConnection con = new SqlConnection("server=(local);Integrated Security=True;database=varatis");
SqlParameter[] str = new SqlParameter[]{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@price", SqlDbType.Int),
new SqlParameter("@image", SqlDbType.Image),
new SqlParameter("@content", SqlDbType.VarChar,200),
new SqlParameter("@state", SqlDbType.Int),
new SqlParameter("@orderid", SqlDbType.Int),
new SqlParameter("@createdate", SqlDbType.DateTime)};
string sql = string.Format("insert into goods(name,price,image,content,state,orderid,createdate) values(@name,@price,@image,@content,@state,@orderid,@createdate)", goodModel.Name, goodModel.Price, goodModel.Image, goodModel.Content, goodModel.State, goodModel.Orderid, goodModel.Createdate);
SqlCommand com = new SqlCommand(sql,con);
com.Parameters.AddRange(str);
con.Open();
int result = com.ExecuteNonQuery();
con.Close();
return result; 运行 int result = com.ExecuteNonQuery();报错 说我缺少@name参数 展开
SqlParameter[] str = new SqlParameter[]{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@price", SqlDbType.Int),
new SqlParameter("@image", SqlDbType.Image),
new SqlParameter("@content", SqlDbType.VarChar,200),
new SqlParameter("@state", SqlDbType.Int),
new SqlParameter("@orderid", SqlDbType.Int),
new SqlParameter("@createdate", SqlDbType.DateTime)};
string sql = string.Format("insert into goods(name,price,image,content,state,orderid,createdate) values(@name,@price,@image,@content,@state,@orderid,@createdate)", goodModel.Name, goodModel.Price, goodModel.Image, goodModel.Content, goodModel.State, goodModel.Orderid, goodModel.Createdate);
SqlCommand com = new SqlCommand(sql,con);
com.Parameters.AddRange(str);
con.Open();
int result = com.ExecuteNonQuery();
con.Close();
return result; 运行 int result = com.ExecuteNonQuery();报错 说我缺少@name参数 展开
1个回答
展开全部
SqlConnection con = new SqlConnection("server=(local);Integrated Security=True;database=varatis");
SqlParameter[] str = new SqlParameter[]{
new SqlParameter("@name",SqlDbType.VarChar,50),
new SqlParameter("@price", SqlDbType.Int),
new SqlParameter("@image", SqlDbType.Image),
new SqlParameter("@content", SqlDbType.VarChar,200),
new SqlParameter("@state", SqlDbType.Int),
new SqlParameter("@orderid", SqlDbType.Int),
new SqlParameter("@createdate", SqlDbType.DateTime)};
// ---- 以下修改需要为:
string sql = "insert into goods(name,price,image,content,state,orderid,createdate) values (@name,@price,@image,@content,@state,@orderid,@createdate)";
str[0].Value = goodModel.Name;
str[1].Value = goodModel.Price;
str[2].Value = goodModel.Image;
// ... 使用上述格式给每个参数赋值 ..., goodModel.Content, goodModel.State,
// goodModel.Orderid,goodModel.Createdate;
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.AddRange(str);
con.Open();
int result = com.ExecuteNonQuery();
con.Close();
return result;
// ---- OK
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询