string User_Id = Session["Userid"].ToString(); //获得登陆用户的ID
string Dd_User = Session["ShouhuoRen"].ToString();//获得姓名
string Dd_Jine = this.Label8.Text;//获得订单总数
string Dd_Addtime = DateTime.Now.ToString();//获得下单时间
string Dd_Bianhao = DateTime.Now.ToString("yyyyMMddhhmmss")+User_Id;//用系统时间+用户ID形成订单号
DataTable mydt = (Session["car"] as DataTable);//绑定gridview数据源
this.GridView1.DataSource = mydt;//绑定gridview数据源
this.GridView1.DataBind();//绑定gridview数据源
for (int i = 0; i < mydt.Rows.Count; i++) //遍历行
{
string Dd_ShangpingId = mydt.Rows[i][0].ToString();//获得订购的商品ID
string Dd_ShangpingSl = mydt.Rows[i][5].ToString();//获得订购的商品数量
string Dd_ShangpingBh = mydt.Rows[i][1].ToString(); //获得订购的商品编号
string sql = "insert into Dingdan (User_Id,Dd_Bianhao,Dd_User,Dd_ShangpingId,Dd_ShangpingSl,Dd_ShangpingBh,Dd_Jine,Dd_Addtime)values('"+User_Id+"','" + Dd_Bianhao +"','"+Dd_User+"','"+Dd_ShangpingId+"','" + Dd_ShangpingSl + "','"+Dd_ShangpingBh+"','"+ Dd_Jine +"','" + Dd_Addtime +"')";
DBclass.ExecSql(sql);//插入数据库
Response.Write("<script>alert('下单成功!');location.href='UserMain.aspx';</script>");
这样当gridview内容有1条数据时,插入数据库时插入的是1条记录,gridview内容有3条数据时,插入数据库时插入的是3条记录。有什么办法:当UserId,Dd_Bianhao,Dd_User是同一个的时候,这3个商品ID保存在同一个Dd_ShangpingId字段内,这3个商品数量保存在同一个Dd_ShangpingSl字段内,这三个商品编号保存在同一个Dd_ShangpingBh字段内?
{
if (dtSource != null)
{
MemoryStream ms = null;
XmlTextWriter XmlWt = null;
try
{
ms = new MemoryStream();
//根据ms实例化XmlWt
XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
//获取ds中的数据
dtSource.WriteXml(XmlWt, XmlWriteMode.WriteSchema); int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
//返回Unicode编码的文本
UnicodeEncoding ucode = new UnicodeEncoding();
string returnValue = ucode.GetString(temp).Trim();
return returnValue;
}
catch (System.Exception ex)
{
//throw ex;
}
finally
{
//释放资源
if (XmlWt != null)
XmlWt.Close();
if (ms != null)
{
ms.Close();
ms.Dispose();
}
}
return "";
}
else
{
return "";
}
} public static DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
DataSet xmlDS;
try
{
xmlDS = new DataSet();
stream = new StringReader(xmlData);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (stream != null)
{
stream.Close();
stream.Dispose();
}
if (reader != null)
reader.Close();
}
return xmlDS;
}