12時間の検索の後、私は自分の問題を解決できません。 SQLでforループが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;
}
は、このコードはアイテムの場合を確認します。..項目がすでにそれはアイテムログにそれに&挿入が更新されますが存在する場合は、データベースをチェックするインサートと第二いずれかの2手順1を作りましたすでにそれは、コードを更新する存在... 更新コードは
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
第二手順:forループを更新から-1の取り外しと私はループのためにコマンドを追加しますが、後
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
その2倍の新アイテムを印刷します?代わりにあなたができる2つのストアドプロシージャの
最初に、例外を再スローする以外の何もしないcatchブロックをコード化しないでください。次に、例外メッセージは何ですか? –
ストアドプロシージャには何がありますか? – Grantly
Insert_Updateの引数が多すぎます – Usama