sqlcommand插入数据的问题
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sqlcon.Open();
string datetime = System.DateTime.Now.ToShortDateString();
string strinsert = "insert into orderinfomain(orderdate,state) values('"+datetime+"','未发货')";
string strinsert1 = "insert into [orderinfomain](recivername,reciverphone,reciveraddress,reciveremail) select truename,memPho,memAdd,memMail from [members] where membersid='"+membersid+"'";
string strinsert2 = "insert into [orderinfomain](price,sumprice,productid) select price,sumprice,productid from [shopcart] where membersid='"+membersid+"' ";
SqlCommand cmd = new SqlCommand(strinsert, sqlcon);
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand(strinsert1, sqlcon);
cmd1.ExecuteNonQuery();
SqlCommand cmd2 = new SqlCommand(strinsert2, sqlcon);
cmd2.ExecuteNonQuery();
sqlcon.Close();
Response.Write("已提交订单,请等待管理员审核!!!");
}
本人比较菜,所以用了3个sqlcommand来向订单表中插入数据 。strinsert字符串向表中插入日期和订单的状态,strinsert1字符串向表中插入收货人的信息(从members【会员表】查询在插入),strinsert2字符串向表中插入商品的信息(从product【商品表中查询在插入】),但是我点击完button后,只有strinsert字符串执行了,其余2个没有执行,这是为什么
------解决方案-------------------- SqlCommand cmd = new SqlCommand(strinsert, sqlcon);
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand(strinsert1, sqlcon);
cmd1.ExecuteNonQuery();
SqlCommand cmd2 = new SqlCommand(strinsert2, sqlcon);
cmd2.ExecuteNonQuery();
这里的问题,应该是因为链接被第一个占用所致,你可以把三个SQL语句拼接到一起然后再执行,就不需要new三次了
------解决方案--------------------是因为对应条件无数据吧
select truename,memPho,memAdd,memMail from [members] where membersid='"+membersid+"'
select price,sumprice,productid from [shopcart] where membersid='"+membersid+"'
****************************