当前位置: 代码迷 >> ASP.NET >> 必须声明标量变量 \"@Proname\,该怎么解决
  详细解决方案

必须声明标量变量 \"@Proname\,该怎么解决

热度:1372   发布时间:2013-02-25 00:00:00.0
必须声明标量变量 \"@Proname\
protected void Button1_Click(object sender, EventArgs e)
  {
  //从窗体中获取值
  string name, id, num, price, sum;
  name = ProName.Text;
  id = ProID.Text;
  num = ProNum.Text;
  price = ProPrice.Text;
  sum = ProSum.Text;

  string sqlstring = "Server=localhost;uid=sa;pwd=2613000;database=Stock";

  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(sqlstring);

  conn.Open();


   
  string insertCommand = "insert into 采购单(采购单号,产品名称,库存数量,成本价格)values(@id,@name,@num,@price)";

   
  System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(insertCommand, conn);
  //执行语句
  cmd.ExecuteNonQuery();
  //关闭连接
  conn.Close();
  Response.Write("<script>alert('写入成功!')</script>");
  }


以上语句出现问题:

用户代码未处理 System.Data.SqlClient.SqlException
  Message="必须声明标量变量 \"@Proname\"。"

请问如何声明标量变量??怎么改,我在网上查了看不大明白??
谢谢先~


------解决方案--------------------------------------------------------
C# code
// 填充参数            SqlParameter para = cmd.Parameters.Add("@id", SqlDbType.NVarChar);            para.Value = id;            para = cmd.Parameters.Add("@name", SqlDbType.NVarChar); // 用具体的字段数据类型替换SqlDbType            para.Value = name;            para = cmd.Parameters.Add("@num", SqlDbType.NVarChar);            para.Value = num;            para = cmd.Parameters.Add("@price", SqlDbType.NVarChar);            para.Value = num;                        //执行语句             cmd.ExecuteNonQuery();                        //关闭连接             conn.Close();
------解决方案--------------------------------------------------------
1,方法一:

C# code
 protected void Button1_Click(object sender, EventArgs e)     {         //从窗体中获取值         string name, id, num, price, sum;         name = ProName.Text;         id = ProID.Text;         num = ProNum.Text;         price = ProPrice.Text;         sum = ProSum.Text;         string sqlstring = "Server=localhost;uid=sa;pwd=2613000;database=Stock";         System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(sqlstring);         conn.Open();         string insertCommand = "insert into 采购单(采购单号,产品名称,库存数量,成本价格)values({0},{1},{2},{3})";         System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(string.Format(insertCommand,id,name,num,price), conn);         //执行语句         cmd.ExecuteNonQuery();         //关闭连接         conn.Close();         Response.Write(" <script>alert('写入成功!') </script>");     }
------解决方案--------------------------------------------------------
C# code
string insertCommand = "insert into 采购单(采购单号,产品名称,库存数量,成本价格)values(@id,@name,@num,@price)"; // 改为:string insertCommand = "insert into 采购单(采购单号,产品名称,库存数量,成本价格) values('" + id + "','" + name + "','" + num + "','" + price + "')";
  相关解决方案