入门记录的ID刚刚插入? [英] Getting ID of record just inserted?
问题描述
我处理用户从一个GridView选择的项目,并执行电子邮件和数据库插入操作。
当用户选择一个按钮时,code以下的信息需要在GridView,会在订单表的新秩序,并会在交易表中新条目。
我怎样才能获得最后插入的ID,如果我使用插入记录的这种方法吗?
你会推荐不同的方法来这个简单的问题?
保护无效btnOrder_Click(对象发件人,EventArgs的发送)
{
双总= 0;
字符串的MailFrom =webadmin@domain.com;
字符串的MailTo =purchasing@domain.com;
字符串MailSubject =在线订购
字符串MailCC =;
字符串MailBCC =;
字符串MailReplyTo =;
字符串MailBody =; 文本框ItmCost code =(文本框)form1.FindControl(txtCost code); 的foreach(在GridView1.Rows GridViewRow GVR)
{
复选框CB =(复选框)gvr.FindControl(ItemSelect);
标签ItmTotal =(标签)gvr.FindControl(ItmTotal);
标签ItmPrice =(标签)gvr.FindControl(ItmPrice);
标签ItmName =(标签)gvr.FindControl(lblName);
文本框ItmQty =(文本框)gvr.FindControl(ItmQty);
文本框ItmID =(文本框)gvr.FindControl(lblItemID); //添加条目顺序表
SqlDataSource2.InsertParameters.Add(订购日期,DateTime.Now.ToString(MMMM DD,YYYY));
SqlDataSource2.InsertParameters.Add(的OrderTotal,0);
SqlDataSource2.InsertParameters.Add(OrderAccount,名);
SqlDataSource2.InsertParameters.Add(OrderCostCentre,ItmCost code.Text);
SqlDataSource2.Insert(); // TODO:GET ORDERID HERE TO用如下:
如果(cb.Checked)
{
双价= Convert.ToDouble(ItmPrice.Text);
双数量= Convert.ToDouble(ItmQty.Text); 总计=价格*数量;
的OrderTotal = +的OrderTotal总; MailBody = MailBody +产品:+ ItmName.Text +数量+ ItmQty.Text +总计:+ ItmTotal.Text +\\ n \\ r; //添加条目事务处理表
SqlDataSource3.InsertParameters.Add(项ID,ItmID.Text);
SqlDataSource3.InsertParameters.Add(订单ID);
SqlDataSource3.InsertParameters.Add(价格,ItmPrice.Text); SqlDataSource3.Insert();
} // TODO:用的OrderTotal更新Order表
} 字符串strOrderTotal = OrderTotal.ToString(); MailBody = MailBody +订单总计:+ strOrderTotal +\\ n \\ r;
MailBody = MailBody +成本code:+ ItmCost code.Text; MailService.Service1梅勒=新MailService.Service1();
Mailer.SendMail(文本,的MailFrom,的MailTo,MailCC,MailBCC,MailSubject,MailBody,MailReplyTo);
}
使用范围的身份在你插入查询结束,它会返回插入ID喜欢...
INSERT INTO表(的ColumnName)VALUES();
走
SELECT SCOPE_IDENTITY();
编辑:您的帮助,这里有一些文章,可以帮助您实施
<一个href=\"http://msdn.microsoft.com/en-us/library/z72eefad.aspx\">http://msdn.microsoft.com/en-us/library/z72eefad.aspx
<一个href=\"http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record\">http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record
<一href=\"http://www.objectreference.net/post/SCOPE_IDENTITY%28%29-return-the-id-from-the-database-on-insert.aspx\">http://www.objectreference.net/post/SCOPE_IDENTITY()-return-the-id-from-the-database-on-insert.aspx
I am processing items users have selected from a gridview, and performing an email and database insert operation.
When the user selects a button, the code below takes information from the gridview, creates a new order in the Order table, and creates new entries in the Transactions table.
How can I get the last inserted ID if I use this method of inserting a record? Would you recommend a different approach to this simple problem?
protected void btnOrder_Click(object sender, EventArgs e)
{
double Total = 0;
string MailFrom = "webadmin@domain.com";
string MailTo = "purchasing@domain.com";
string MailSubject = "Online Order";
string MailCC = "";
string MailBCC = "";
string MailReplyTo = "";
string MailBody = "";
TextBox ItmCostCode = (TextBox)form1.FindControl("txtCostCode");
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl("ItemSelect");
Label ItmTotal = (Label)gvr.FindControl("ItmTotal");
Label ItmPrice = (Label)gvr.FindControl("ItmPrice");
Label ItmName = (Label)gvr.FindControl("lblName");
TextBox ItmQty = (TextBox)gvr.FindControl("ItmQty");
TextBox ItmID = (TextBox)gvr.FindControl("lblItemID");
//Add entry to Order Table
SqlDataSource2.InsertParameters.Add("OrderDate", DateTime.Now.ToString("MMMM dd, yyyy"));
SqlDataSource2.InsertParameters.Add("OrderTotal", "0");
SqlDataSource2.InsertParameters.Add("OrderAccount", "name");
SqlDataSource2.InsertParameters.Add("OrderCostCentre", ItmCostCode.Text);
SqlDataSource2.Insert();
//TODO: GET ORDERID HERE TO USE BELOW:
if (cb.Checked)
{
double Price = Convert.ToDouble(ItmPrice.Text);
double Qty = Convert.ToDouble(ItmQty.Text);
Total = Price * Qty;
OrderTotal = OrderTotal + Total;
MailBody = MailBody + "Item: "+ItmName.Text+" Quantity: "+ItmQty.Text+" Total: "+ItmTotal.Text+"\n\r";
//Add entry to Transaction Table
SqlDataSource3.InsertParameters.Add("ItemID", ItmID.Text);
SqlDataSource3.InsertParameters.Add("OrderID", );
SqlDataSource3.InsertParameters.Add("Price", ItmPrice.Text);
SqlDataSource3.Insert();
}
//TODO: Update Order table with OrderTotal
}
string strOrderTotal = OrderTotal.ToString();
MailBody = MailBody+"Order Total: " + strOrderTotal+"\n\r";
MailBody = MailBody + "Cost Code: " + ItmCostCode.Text;
MailService.Service1 Mailer = new MailService.Service1();
Mailer.SendMail("Text", MailFrom, MailTo, MailCC, MailBCC, MailSubject, MailBody, MailReplyTo);
}
use scope identity at the end of your insert query and it will return inserted ID like...
INSERT INTO table (ColumnName) VALUES ();
GO
SELECT SCOPE_IDENTITY();
Edit: for your help, here is some article that can help you to implement
http://msdn.microsoft.com/en-us/library/z72eefad.aspx
http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record http://www.objectreference.net/post/SCOPE_IDENTITY()-return-the-id-from-the-database-on-insert.aspx
这篇关于入门记录的ID刚刚插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!