参数化查询 需要参数 但未提供该参数.SqlConnection con = new SqlConnection("server=(local);Integrated Security=True;database=varatis"); SqlParameter[] str = new SqlParameter[]{ new SqlParameter("@name",SqlDbType.Va

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/01 06:44:15

参数化查询 需要参数 但未提供该参数.SqlConnection con = new SqlConnection("server=(local);Integrated Security=True;database=varatis"); SqlParameter[] str = new SqlParameter[]{ new SqlParameter("@name",SqlDbType.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参数

参数化查询 需要参数 但未提供该参数.SqlConnection con = new SqlConnection("server=(local);Integrated Security=True;database=varatis"); SqlParameter[] str = new SqlParameter[]{ new SqlParameter("@name",SqlDbType.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 = "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