for循环仅运行两次

时间:2017-10-26 21:01:21

标签: c# sql-server

经过12个小时的搜索,我无法解决问题。

在SQL中我为插入创建了2个过程,第二个过程将检查数据库是否已存在项目它将更新&在项目日志中插入..

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

    for (int i = 0; i < dgv.Rows.Count; i++)
    {
        SqlCommand cmd = new SqlCommand("Select * From Purchase_Order WHERE Item_Name = '" + dgv.Rows[i].Cells["Column2"].Value + "' and Seller_Name = '" + dgv.Rows[i].Cells["Column3"].Value + "' and Company_Name = '" + dgv.Rows[i].Cells["Column4"].Value + "' ", con);
        SqlDataAdapter ds = new SqlDataAdapter(cmd);
        DataSet da = new DataSet();
        ds.Fill(da);
        int j = da.Tables[0].Rows.Count;
        if (j > 0)
        {
            update(dgv, Date_of_Purchase, Discount, Paid_Amount, lbl_Remaining, lbl_Subtotal, lbl_Total);
        }
        else
        {
            SqlCommand sc = new SqlCommand("Purchase_Order_History", con);
            sc.CommandType = CommandType.StoredProcedure;
            sc.Parameters.AddWithValue("@Invoice_no", dgv.Rows[i].Cells["Column1"].Value);
            sc.Parameters.AddWithValue("@Date_of_Purchase", Date_of_Purchase.Value.ToString("yyyy/MM/dd"));
            sc.Parameters.AddWithValue("@Item_Name", dgv.Rows[i].Cells["Column2"].Value);
            sc.Parameters.AddWithValue("@Seller_Name", dgv.Rows[i].Cells["Column3"].Value);
            sc.Parameters.AddWithValue("@Company_Name", dgv.Rows[i].Cells["Column4"].Value);
            sc.Parameters.AddWithValue("@Quantity", dgv.Rows[i].Cells["Column5"].Value);
            sc.Parameters.AddWithValue("@Unit_Price", dgv.Rows[i].Cells["Column6"].Value);
            sc.Parameters.AddWithValue("@Total_Price", dgv.Rows[i].Cells["Column7"].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.ExecuteNonQuery();
        }
    }

    SuccessBox sb = new SuccessBox();
    sb.ShowDialog();
    con.Close();
}
catch (Exception)
{
    throw;
}

此代码检查项目是否已存在然后它将更新代码.. 更新代码在这里......

try
{
    SqlConnection con = new SqlConnection(str);
    con.Open();
    for (int i = 0; i < dgv.Rows.Count; i++)
    {
        SqlCommand sc = new SqlCommand("Update_Insert", con);
        sc.CommandType = CommandType.StoredProcedure;
        sc.Parameters.AddWithValue("@Invoice_no", dgv.Rows[i].Cells["Column1"].Value);
        sc.Parameters.AddWithValue("@Date_of_Purchase", Date_of_Purchase.Value.ToString("yyyy/MM/dd"));
        sc.Parameters.AddWithValue("@Item_Name", dgv.Rows[i].Cells["Column2"].Value);
        sc.Parameters.AddWithValue("@Seller_Name", dgv.Rows[i].Cells["Column3"].Value);
        sc.Parameters.AddWithValue("@Company_Name", dgv.Rows[i].Cells["Column4"].Value);
        sc.Parameters.AddWithValue("@Quantity", dgv.Rows[i].Cells["Column5"].Value);
        sc.Parameters.AddWithValue("@Unit_Price", dgv.Rows[i].Cells["Column6"].Value);
        sc.Parameters.AddWithValue("@Total_Price", dgv.Rows[i].Cells["Column7"].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.ExecuteNonQuery();
        i++;
    }

    con.Close();
}
catch (Exception)
{
    throw;
}

实际上我的问题是,当我输入数据库中已经存在的相同项目时,它会更新它,但是如果我用它添加新项目,它将只添加1个新项目,然后它就会抛出。

任何解决方案都会有所帮助。

我的存储过程Purchase_Order_history:

ALTER PROCEDURE [dbo].[Purchase_Order_History]
(
@Invoice_no int,
@Date_of_Purchase date,
@Item_Name nvarchar(100),
@Seller_Name nvarchar(100),
@Company_Name nvarchar(100),
@Quantity int,
@Unit_Price int,
@Total_Price int,
@Discount int,
@Paid_Amount int,
@Remaining int,
@Sub_Total int,
@Total int,
@Selling_Price int = null
)

AS
BEGIN

INSERT INTO Purchase_Order(Invoice_no, Date_of_Purchase, Item_Name, 
Seller_Name, Company_Name, Quantity, Unit_Price, Total_Price, Discount, 
Paid_Amount, Remaining, Sub_Total, Total) 
VALUES(@Invoice_no, @Date_of_Purchase,@Item_Name, @Seller_Name, 
@Company_Name, @Quantity,@Unit_Price, @Total_Price, @Discount, @Paid_Amount, 
@Remaining, @Sub_Total, @Total);

INSERT INTO Purchase_Order_Log(Invoice_no, Date_of_Purchase, Item_Name, 
Seller_Name, Company_Name, Quantity, Unit_Price, Total_Price, Discount, 
Paid_Amount, Remaining, Sub_Total, Total) 
VALUES(@Invoice_no, @Date_of_Purchase,@Item_Name, @Seller_Name, 
@Company_Name, @Quantity,@Unit_Price, @Total_Price, @Discount, @Paid_Amount, 
@Remaining, @Sub_Total, @Total);

INSERT INTO Items(Date_of_Purchase, Item_Name, Seller_Name, Company_Name, 
Available_Stock, Unit_Price, Total_Price, Selling_Price) 
VALUES(@Date_of_Purchase,@Item_Name, @Seller_Name, @Company_Name, 
@Quantity,@Unit_Price, @Total_Price, @Selling_Price);


END

第二程序:

ALTER PROCEDURE [dbo].[Update_Insert]
(
@Invoice_no int,
@Date_of_Purchase date,
@Item_Name nvarchar(100),
@Seller_Name nvarchar(100),
@Company_Name nvarchar(100),
@Quantity int,
@Unit_Price int,
@Total_Price int,
@Discount int,
@Paid_Amount int,
@Remaining int,
@Sub_Total int,
@Total int
)

AS
BEGIN

INSERT INTO Purchase_Order_Log(Invoice_no, Date_of_Purchase, Item_Name, 
Seller_Name, Company_Name, Quantity, Unit_Price, Total_Price, Discount, 
Paid_Amount, Remaining, Sub_Total, Total) 
VALUES(@Invoice_no, @Date_of_Purchase,@Item_Name, @Seller_Name, 
@Company_Name, @Quantity,@Unit_Price, @Total_Price, @Discount, @Paid_Amount, 
@Remaining, @Sub_Total, @Total);

UPDATE Purchase_Order SET Quantity = Quantity + @Quantity, Unit_Price = 
@Unit_Price, Total_Price = Total_Price + @Total_Price Where Item_Name = 
@Item_Name AND Company_Name = @Company_Name AND Seller_Name = @Seller_Name

UPDATE Items SET Available_Stock = Available_Stock + @Quantity, Unit_Price = 
@Unit_Price, Total_Price = Total_Price + @Total_Price Where Item_Name = 
@Item_Name AND Company_Name = @Company_Name AND Seller_Name = @Seller_Name

END

从更新循环中删除-1后,我将命令添加到for循环中,但是它打印新项目2次?

1 个答案:

答案 0 :(得分:0)

而不是两个存储过程,您可以使用

IF NOT EXISTS (SELECT 1 FROM table_Name WHERE condition) 
BEGIN 
INSERT INTO table_Name (id,name) 
VALUES 
(@id, @name)
END
ELSE 
UPDATE table_Name SET id = @id, name = @name 
WHERE condition

我正在使用此示例结构,因为您的查询非常长。将示例查询转换为您的查询并尝试执行它将对您有所帮助