日期:2014-05-18 浏览次数:20473 次
// 填充参数 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,方法一:
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>"); }
------解决方案--------------------
string insertCommand = "insert into 采购单(采购单号,产品名称,库存数量,成本价格)values(@id,@name,@num,@price)"; // 改为: string insertCommand = "insert into 采购单(采购单号,产品名称,库存数量,成本价格) values('" + id + "','" + name + "','" + num + "','" + price + "')";