[英]Inserting into two tables C# & SQL Server
I am trying to develop a small POS application, and I need to do some data insertion into database, but I am having troubles inserting into two tables at once.我正在尝试开发一个小型 POS 应用程序,我需要向数据库中插入一些数据,但是我无法同时插入两个表。
I am using this stored procedure:我正在使用这个存储过程:
// insertpurchase stored procedure
BEGIN TRANSACTION
DECLARE @id int;
INSERT INTO tblsuppling_general (Supplier, Bill_nr, Date, Total, Note)
VALUES (@supplier, @billnr, @date, @total, @note)
SELECT @id = SCOPE_IDENTITY(); // here I want to get ID of the insert of the supply and use on every row in second table
INSERT INTO tblpurchase_details (ID_of_supplying, Barcode, Name, Category, Stock, VAT, QTY, Unit, Supplying_price, Price, Earn_pcs, Bill_nr)
VALUES (@id, @barcode, @name, @category, @stock, @vat, 1, @unit, @sup_price, @price, @earn, @billnr)
COMMIT
And the C# code:和 C# 代码:
try
{
conn.Open();
foreach (DataGridViewRow row in dtgartikulli.Rows)
{
if (!row.IsNewRow)
{
SqlCommand cmd = new SqlCommand("insertpurchase", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@supplier", cmbsupplier.Text));
cmd.Parameters.Add(new SqlParameter("@billnr", txtbillnr.Text));
cmd.Parameters.Add(new SqlParameter("@date", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("@total", txttotali.Text));
cmd.Parameters.Add(new SqlParameter("@note", txtnote.Text));
cmd.Parameters.Add(new SqlParameter("@barcode", row.Cells[0].Value));
cmd.Parameters.Add(new SqlParameter("@stock", row.Cells[3].Value));
cmd.Parameters.Add(new SqlParameter("@price", row.Cells[2].Value));
cmd.Parameters.Add(new SqlParameter("@sup_price", row.Cells[4].Value));
cmd.Parameters.Add(new SqlParameter("@earn", row.Cells[1].Value));
cmd.Parameters.Add(new SqlParameter("@fitimi", row.Cells[5].Value));
cmd.Parameters.Add(new SqlParameter("@category", row.Cells[6].Value));
cmd.Parameters.Add(new SqlParameter("@unit", row.Cells[9].Value));
cmd.Parameters.Add(new SqlParameter("@vat", row.Cells[7].Value));
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Insertion of transaction failed" + ex.ToString());
}
finally
{
foreach (DataGridViewRow row in dtgartikulli.Rows)
{
if (!row.IsNewRow)
{
SqlCommand cmd = new SqlCommand("insertproducts", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@barcode", row.Cells[0].Value));
cmd.Parameters.Add(new SqlParameter("@stock", row.Cells[3].Value));
cmd.Parameters.Add(new SqlParameter("@price", row.Cells[2].Value));
cmd.Parameters.Add(new SqlParameter("@sup_price", row.Cells[4].Value));
cmd.Parameters.Add(new SqlParameter("@earn", row.Cells[1].Value));
cmd.Parameters.Add(new SqlParameter("@fitimi", row.Cells[5].Value));
cmd.Parameters.Add(new SqlParameter("@category", row.Cells[6].Value));
cmd.Parameters.Add(new SqlParameter("@unit", row.Cells[9].Value));
cmd.Parameters.Add(new SqlParameter("@vat", row.Cells[7].Value));
cmd.ExecuteNonQuery();
}
}
conn.Close();
fshi();
MessageBox.Show("Transaction is saved successfully");
}
The insert is done, but I want it to be made in a relationship of one to many.插入已完成,但我希望它以一对多的关系进行。 For example in table one only one row with general information of the bill, and in the second table the details of the bill(products information).
例如在表一中只有一行包含账单的一般信息,而在第二个表中是账单的详细信息(产品信息)。 With the above code I am getting in table one as much rows as in table two.
使用上面的代码,我在表一中的行数与表二中的行数一样多。 For example if I have four rows with product information in table two, I am getting same repeated data in four rows in table with general data of the purchase bill, instead of just one row.
例如,如果我在表 2 中有四行包含产品信息,那么我在表中的四行中获得相同的重复数据,其中包含采购账单的一般数据,而不是只有一行。 My question is what to do, in order to achieve a one to many insertion(one row for general info of purchase- table) and (many rows for purchased product info-table)
我的问题是要做什么,以实现一对多插入(一行用于购买表的一般信息)和(多行用于购买的产品信息表)
Thanks in advance提前致谢
You can break your procedure in two, one for the tblsuppling_general
and another for the tblpurchase_details
.您可以将程序分成两部分,一个用于
tblsuppling_general
,另一个用于tblpurchase_details
。
Or you can change your current procedure to:或者您可以将当前程序更改为:
IF NOT EXISTS (
SELECT 1 FROM tblsuppling_general WHERE
)
BEGIN
INSERT INTO tblsuppling_general (Supplier, Bill_nr, Date, Total, Note)
VALUES (@supplier, @billnr, @date, @total, @note)
END
etc...
But be aware that this kind of insert-if-not-exists is problematic under heavy load because the execution of IF NOT EXISTS
and INSERT INTO
are not atomic.但请注意,这种 insert-if-not-exists 在重负载下是有问题的,因为
IF NOT EXISTS
和INSERT INTO
的执行不是原子的。
Personally, I recommend using two procedures.就个人而言,我建议使用两个程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.