我使用存储过程将记录插入到两个表中,但此刻只插入一个表中。在调试中,它不会进入try区域以便将数据存储在另一个表中。这些是两个存储过程以及用于调用存储过程的方法
存储过程:
ALTER PROCEDURE [dbo].[createRequestedItem]
@expense_acc varchar(50),
@quantity_requested int,
@inventory_acc varchar(50),
@Req_No varchar(50),
@Item_No varchar (50),
@unit_of_measure varchar(15),
@issue_price float
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO RequisitionItem
VALUES (@expense_acc, @quantity_requested, @inventory_acc, @Req_No, @Item_No,
@unit_of_measure, @issue_price)
END
ALTER PROCEDURE [dbo].[requisition_sp_createRequisition]
@Req_No varchar(50),
@Req_Date datetime,
--@description varchar (50),
--@quantity int,
@employee_id nvarchar (50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Requisition
VALUES (@Req_No, @Req_Date,
--@description,
--@quantity,
@employee_id)
END
模特课:
public bool createRequisition(Requisition requisition, Item requisitionitem)
{
int rec_upd = 0;
bool status;
using (SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString()))
{
SqlCommand command = new SqlCommand();
SqlCommand requestcommand = new SqlCommand();
//Command to update the requisition table with the header information
requestcommand.Connection = TWCLOPConnect;
requestcommand.CommandType = CommandType.StoredProcedure;
requestcommand.CommandText = "requisition_sp_createRequisition";
requestcommand.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = requisition.reqNumber;
requestcommand.Parameters.Add("@Req_Date", SqlDbType.DateTime).Value = requisition.reqDate;
requestcommand.Parameters.Add("@employee_id", SqlDbType.VarChar).Value = requisition.employeeDetails.employeeNum;
//Command to update the issueitem table with details of the transaction
command.Connection = TWCLOPConnect;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "createRequestedItem";
command.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = requisition.reqNumber;
command.Parameters.Add("@inventory_acc", SqlDbType.Int).Value = requisition.inventory_account;
command.Parameters.Add("@item_no", SqlDbType.VarChar);
command.Parameters.Add("@quantity_requested", SqlDbType.Float);
command.Parameters.Add("@issue_price", SqlDbType.Float);
command.Parameters.Add("@expense_acc", SqlDbType.Int);
command.Parameters.Add("@unit_of_measure", SqlDbType.VarChar);
try
{
TWCLOPConnect.Open();
requestcommand.ExecuteNonQuery();
foreach (var item in requisition.items)
{
command.Parameters["@itemnum"].Value = item.itemNumber;
command.Parameters["@issue_price"].Value = item.price;
command.Parameters["@qty"].Value = item.quantity;
command.Parameters["@expense_acc"].Value = item.expense_account.index;
command.Parameters["@unit_of_measure"].Value = item.selecteduomtext.Trim();
rec_upd = command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
TWCLOPConnect.Close();
}
if (rec_upd > 0)
{
status = true;
}
else
{
status = false;
}
}
return status;
}