[英]Give an auto-generated ID in consecutive SQL Queries as a column value for another table
I have two tables , namely : tblEventService
and tblEventItem
.. 我有两个表,即: tblEventService
和tblEventItem
..
Following are the columns of both : 以下是这两列:
This is the method i am using to insert into the two tables as suggested by @M.Ali in the answer : 这是我用来在两个表中插入@ M.Ali在答案中建议的方法:
public int concurrentInsert(EventService eventServiceObj, EventItem eventItem)
{
string query = @" Declare @EventServiceID INT;
insert into tblEventService(ServiceDate,ServiceVenue,Status,CustomerRemarks,ServiceID,VendorID,EventID)
values(@ServiceDate,@ServiceVenue,@Status,@CustomerRemarks,@ServiceID,@VendorID,@EventID);
SELECT @EventServiceID = SCOPE_IDENTITY();
INSERT INTO TableEventItem(EventServiceID,VendorItemID,Price,Quantity,CustomerRemarks)
VALUES (@EventServiceID , @VendorItemID , @Price,@Quantity,@CustomerRemarks)";
List<SqlParameter> lstParams = new List<SqlParameter>();
lstParams.Add(new SqlParameter("@ServiceDate", eventServiceObj.ServiceDate));
lstParams.Add(new SqlParameter("@ServiceVenue", eventServiceObj.ServiceVenue));
lstParams.Add(new SqlParameter("@Status", eventServiceObj.Status));
lstParams.Add(new SqlParameter("@CustomerRemarks", eventServiceObj.CustomerRemarks));
lstParams.Add(new SqlParameter("@ServiceID", eventServiceObj.ServiceID));
lstParams.Add(new SqlParameter("@VendorID", eventServiceObj.VendorID));
lstParams.Add(new SqlParameter("@EventID", eventServiceObj.EventID));
lstParams.Add(new SqlParameter("@EventServiceID", eventItem.EventServiceID));
lstParams.Add(new SqlParameter("@VendorItemID", eventItem.VendorItemID));
lstParams.Add(new SqlParameter("@Price", eventItem.Price));
lstParams.Add(new SqlParameter("@Quantity", eventItem.Quantity));
lstParams.Add(new SqlParameter("@CustomerRemarks", eventItem.CustomerRemarks));
The query works fine when i checked it by putting values hard-coded . 当我通过将值硬编码检查时,查询工作正常。 Now i have one problem : 现在我有一个问题:
the autogenerated ID @EventServiceID should be provided in the insert method.. how do i give that in C# code ? 自动生成的ID @EventServiceID应该在insert方法中提供。如何在C#代码中提供该ID?
This is the method in C# code i am using : 这是我正在使用的C#代码中的方法:
for (int i = 0; i < rptr.Items.Count; i++)
{
EventService eventService = new EventService();
EventServiceLogic eventServiceLogic = new EventServiceLogic();
eventService.ServiceID = Convert.ToInt32(((HiddenField)rptr.Items[i].FindControl("hdnServiceID")).Value);
eventService.VendorID = Convert.ToInt32(((HiddenField)rptr.Items[i].FindControl("HiddenField1")).Value);
TextBox txtDate = (TextBox)rptr.Items[i].FindControl("txtServiceDate");
DateTime eventDate = new DateTime();
if (DateTime.TryParseExact(txtDate.Text, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out eventDate))
{
eventService.ServiceDate = eventDate;
}
else
{
ScriptManager.RegisterStartupScript(lnkBtnOrder, lnkBtnOrder.GetType(), "key", "alert('Invalid Date!Please enter in the format : Date-Month-Year')", true);
return;
}
eventService.ServiceVenue = txtVenueName.Text;
eventService.CustomerRemarks = txtRemarks.Text;
eventService.Status = "pending";
eventService.EventID = Convert.ToInt32(ddlMyEvents.SelectedValue);
EventItem eventItem = new EventItem();
eventItem.VendorItemID = Convert.ToInt32(((HiddenField)rptr.Items[i].FindControl("hdnVendorItemID")).Value);
eventItem.CustomerRemarks = eventService.CustomerRemarks = txtRemarks.Text;
eventItem.Quantity = Convert.ToInt32(((TextBox)rptr.Items[i].FindControl("txtQty")).Text);
eventItem.EventServiceID = //this is auto-generated. how should i provide this value ?
int registerServiceResult = eventServiceLogic.concurrentInsert(eventService,eventItem);
if (registerServiceResult > 0)
{
lblTotal.Text = "This Works!";
}
else
{
lblTotal.Text = "This Doesn't Work!";
}
}
Google system function SCOPE_IDENTITY()
and you would write your two inserts like this... Google系统函数SCOPE_IDENTITY()
,您将这样编写两个插入内容...
Single row Insert 单行插入
Declare @EventServiceID INT;
insert into tblEventService(ServiceDate,ServiceVenue,Status,CustomerRemarks,ServiceID,VendorID,EventID)
values(@ServiceDate,@ServiceVenue,@Status,@CustomerRemarks,@ServiceID,@VendorID,@EventID);
SELECT @EventServiceID = SCOPE_IDENTITY();
INSERT INTO TableEventItem(EventServiceID , Col2 , Col3 , ....)
VALUES (@EventServiceID , @var2 , @var3 , ....)
Multiple rows Insert 多行插入
If you are doing multiple inserts you would do something like.... 如果您要进行多个插入操作,则可能会执行以下操作...。
Declare @EventSrvcID_Table TABLE
(EventServiceID INT,
Col1 [Datatype],
Col2 [Datatype]) ;
insert into tblEventService(ServiceDate,ServiceVenue,Status,CustomerRemarks,ServiceID,VendorID,EventID)
OUTPUT inserted.EventServiceID , inserted.Col1 , inserted.Col2
INTO @EventSrvcID_Table(EventServiceID , Col2 , Col3)
values(@ServiceDate1,@ServiceVenue1,@Status1,@CustomerRemarks1,@ServiceID1,@VendorID1,@EventID1)
,(@ServiceDate2,@ServiceVenue2,@Status2,@CustomerRemarks2,@ServiceID2,@VendorID2,@EventID2);
INSERT INTO TableEventItem(EventServiceID , Col2 , Col3 , ....)
SELECT EventServiceID , Col2 , Col3
FROM @EventSrvcID_Table
What I needed : An auto-generated id , which is created while inserting into one table and that id is to be used in as a column value in other table. 我需要的是:自动生成的ID,它是在插入一个表时创建的,并且该ID将用作另一表中的列值。
Table Names: 1)tblEventService (auto-generated id : EventServiceID
) 2)tblEventItem 表名称: 1)tblEventService(自动生成的ID: EventServiceID
)2)tblEventItem
What I Did: Method for tblEventService : 我做了什么: tblEventService的方法:
public int Insert(EventService eventServiceObj)
{
string query = "insert into tblEventService(ServiceDate,ServiceVenue,Status,CustomerRemarks,ServiceID,VendorID,EventID) values(@ServiceDate,@ServiceVenue,@Status,@CustomerRemarks,@ServiceID,@VendorID,@EventID)";
List<SqlParameter> lstParams = new List<SqlParameter>();
lstParams.Add(new SqlParameter("@ServiceDate", eventServiceObj.ServiceDate));
lstParams.Add(new SqlParameter("@ServiceVenue", eventServiceObj.ServiceVenue));
.....
//similarly add other parameters too
DBUtility.ModifyData(query, lstParams); //this query is an Insert Query
string query1 = "select EventServiceID from tblEventService where ServiceDate=@ServiceDate and ServiceVenue=@ServiceVenue and Status=@Status and CustomerRemarks=@CustomerRemarks and ServiceID=@ServiceID and VendorID=@VendorID and EventID = @EventID"; //this query selects the EventServiceID of the data inserted by the corressponding User . This will make sure to select correct data even if there are concurrent entries.
List<SqlParameter> lstParams1 = new List<SqlParameter>();
lstParams1.Add(new SqlParameter("@ServiceDate", eventServiceObj.ServiceDate));
lstParams1.Add(new SqlParameter("@ServiceVenue", eventServiceObj.ServiceVenue));
//similarly add other parameters too
DataTable dt = DBUtility.SelectData(query1, lstParams1);
return Convert.ToInt32(dt.Rows[0]["EventServiceID"]);
} //this will return the autogenerated id which i require in the other table!
Hence, what is did was Insert and then Select !This worked for me..!One of the answers also worked as a query
but i had problem giving that parameter in the other table! 因此,执行的操作是插入 ,然后选择 !这对我有用。。!答案之一也可以作为query
但是我在另一个表中给出该参数时遇到问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.