如何使用c#Windows窗体更新现有记录并插入新记录(如果不存在)

时间:2017-11-06 10:02:59

标签: c# sql-server datagridview

首先,我使用的是Windows Form C#和SQL Server 2014

我有更新表格,见下图

See Image

这是数据库和datagridview中的现有记录,如果我添加新记录,我想更新现有记录并在数据库中插入新记录,以下只是更新现有记录,如果我添加它不会插入新记录它在datagridview

See This Image What I Want

我的C#代码

try
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();

            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                SqlCommand sc = new SqlCommand("Update_Purchase", con);
                sc.CommandType = CommandType.StoredProcedure;
                sc.Parameters.AddWithValue("@Invoice_no", Invoice_No.Text);
                sc.Parameters.AddWithValue("@Date_of_Purchase", Date_of_Purchase.Value.ToString("yyyy/MM/dd"));
                sc.Parameters.AddWithValue("@Item_Code", dgv.Rows[i].Cells["Column1"].Value);
                sc.Parameters.AddWithValue("@Item_Name", dgv.Rows[i].Cells["Column2"].Value);
                sc.Parameters.AddWithValue("@Descriptin", dgv.Rows[i].Cells["Column7"].Value);
                sc.Parameters.AddWithValue("@Supplier_Name", Supplier_Name.Text);
                sc.Parameters.AddWithValue("@Company_Name", Company_Name.Text);
                sc.Parameters.AddWithValue("@Quantity", dgv.Rows[i].Cells["Column3"].Value);
                sc.Parameters.AddWithValue("@Unit_Price", dgv.Rows[i].Cells["Column4"].Value);
                sc.Parameters.AddWithValue("@Total_Price", dgv.Rows[i].Cells["Column5"].Value);
                sc.Parameters.AddWithValue("@Selling_Price", dgv.Rows[i].Cells["Column6"].Value);
                sc.Parameters.AddWithValue("@Discount", Discount.Text);
                sc.Parameters.AddWithValue("@Paid_Amount", Paid_Amount.Text);
                sc.Parameters.AddWithValue("@Remaining", lbl_Remaining.Text);
                sc.Parameters.AddWithValue("@Sub_Total", lbl_Subtotal.Text);
                sc.Parameters.AddWithValue("@Total", lbl_Total.Text);
                sc.Parameters.AddWithValue("@Updated_Date", Updated_Date.Value.ToString("yyyy/MM/dd"));

                sc.ExecuteNonQuery();
            }
            con.Close();
            SuccessBox sb = new SuccessBox();
            sb.label1.Text = "Updated Successfully";
            sb.ShowDialog();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

我的存储过程:

ALTER PROCEDURE [dbo].[Update_Purchase]
(
@Invoice_no int,
@Date_of_Purchase date,
@Item_Code int,
@Item_Name Nvarchar(100),
@Descriptin Nvarchar(MAX),
@Supplier_Name Nvarchar(100),
@Company_Name Nvarchar(100),
@Quantity int,
@Unit_Price int,
@Total_Price int,
@Selling_Price int,
@Discount int,
@Paid_Amount int,
@Remaining int,
@Sub_Total int,
@Total int,
@Updated_Date date
)

AS
BEGIN

UPDATE Purchase_Order_Log SET Invoice_no = @Invoice_no, Date_of_Purchase = 
@Date_of_Purchase, Item_Code = @Item_Code, Item_Name = @Item_Name, 
Descriptin = @Descriptin, Supplier_Name = @Supplier_Name, Company_Name = 
@Company_Name, Quantity = @Quantity, Unit_Price = @Unit_Price, Total_Price = 
@Total_Price, Selling_Price = @Selling_Price, Discount = @Discount, 
Paid_Amount = @Paid_Amount, Remaining = @Remaining, Sub_Total = @Sub_Total, 
Total = @Total, Updated_Date = @Updated_Date 
WHERE Invoice_no = @Invoice_no AND Item_Code = @Item_Code
END

2 个答案:

答案 0 :(得分:0)

修改您的商店程序,如

ALTER PROCEDURE [dbo].[Update_Purchase]
(
@Invoice_no int,
@Date_of_Purchase date,
@Item_Code int,
@Item_Name Nvarchar(100),
@Descriptin Nvarchar(MAX),
@Supplier_Name Nvarchar(100),
@Company_Name Nvarchar(100),
@Quantity int,
@Unit_Price int,
@Total_Price int,
@Selling_Price int,
@Discount int,
@Paid_Amount int,
@Remaining int,
@Sub_Total int,
@Total int,
@Updated_Date date
)

AS
BEGIN



    IF NOT EXISTS (SELECT 1 FROM Purchase_Order_Log WHERE Invoice_no = @Invoice_no AND Item_Code = @Item_Code) 
    BEGIN 
    INSERT INTO Purchase_Order_Log (Invoice_no , Date_of_Purchase, Item_Code, Item_Name, Descriptin, Supplier_Name, Company_Name,
    Quantity, Unit_Price, Total_Price, Selling_Price, Discount, Paid_Amount, Remaining, Sub_Total, Total, Updated_Date) 
    VALUES 
    (@Invoice_no, @Date_of_Purchase, @Item_Code, @Item_Name, @Descriptin, @Supplier_Name, @Company_Name,
     @Quantity, @Unit_Price, @Total_Price, @Selling_Price, @Discount, @Paid_Amount, @Remaining, @Sub_Total,Total, @Updated_Date)
    END
    ELSE 
    UPDATE Purchase_Order_Log SET Invoice_no = @Invoice_no, Date_of_Purchase = 
    @Date_of_Purchase, Item_Code = @Item_Code, Item_Name = @Item_Name, 
    Descriptin = @Descriptin, Supplier_Name = @Supplier_Name, Company_Name = 
    @Company_Name, Quantity = @Quantity, Unit_Price = @Unit_Price, Total_Price = 
    @Total_Price, Selling_Price = @Selling_Price, Discount = @Discount, 
    Paid_Amount = @Paid_Amount, Remaining = @Remaining, Sub_Total = @Sub_Total, 
    Total = @Total, Updated_Date = @Updated_Date 
    WHERE Invoice_no = @Invoice_no AND Item_Code = @Item_Code

根据您的要求修改IF NOT EXISTS条件

答案 1 :(得分:0)

您可以使用SQL Server Merge使用单个语句来完成此操作。与此相似

MERGE dbo.Test WITH (SERIALIZABLE) AS T
USING (VALUES (3012, 'john')) AS U (id, name)
    ON U.id = T.id
WHEN MATCHED THEN 
    UPDATE SET T.name = U.name
WHEN NOT MATCHED THEN
    INSERT (id, name) 
    VALUES (U.id, U.name);

这里它检查Table Test上的列ID的值3012,如果找到匹配则更新记录,否则插入相同的记录。