如何在按钮单击事件ADO.NET

时间:2017-02-25 05:27:43

标签: c# sql-server ado.net

这是我将玩具添加到购物车后购买玩具的存储程序

CREATE PROCEDURE PurchaseToys
    @ToyPurhcaseNo UNIQUEIDENTIFIER OUTPUT,
    @CartNo UNIQUEIDENTIFIER,
    @ToyNo UNIQUEIDENTIFIER,
    @MemberNo UNIQUEIDENTIFIER ,
    @TotalQuantity INTEGER,
    @TotalPrice INTEGER,
    @CreditCardNo BIGINT,
    @PurchaseDate DATETIME OUTPUT
AS
BEGIN
    SET @ToyPurhcaseNo = NEWID()
    SET @PurchaseDate = GETDATE()

    INSERT INTO Toy_Purchase(Toy_Purchase_No, Cart_No, Toy_No, Member_No, Total_Quantity, Total_Price, Credit_Card_No, Purchase_Date)
    VALUES(@ToyPurhcaseNo, @CartNo, @ToyNo, @MemberNo, @TotalQuantity, @TotalPrice, @CreditCardNo, @PurchaseDate)

    UPDATE Toy
    SET Quantity = Quantity - Total_Price 
    FROM Toy, Toy_Purchase
    WHERE Toy.Toy_No = Toy_Purchase.Toy_No
END

这是我在CS文件中的ADO.NET代码

 protected void Purchase_Click(object sender,EventArgs e )
 {
            string CartId = row.Cells[0].Text;
            string ToyId = row.Cells[1].Text;
            int TotalQuantity = Convert.ToInt32(row.Cells[4].Text);
            int TotalPrice = Convert.ToInt32(row.Cells[5].Text);

            string CS1;
            CS1 = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Toy_WebForm; integrated security=SSPI";
            SqlConnection con1 = new SqlConnection(CS1);
            SqlTransaction Transaction = con1.BeginTransaction();
            SqlCommand cmd1 = new SqlCommand("PurchaseToys", con1, Transaction);
            cmd1.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@CartNo", CartId);
            cmd.Parameters.AddWithValue("@ToyNo", ToyId);
            cmd.Parameters.AddWithValue("@MemberNo", MemberId);
            cmd.Parameters.AddWithValue("@TotalQuantity", TotalQuantity);
            cmd.Parameters.AddWithValue("@TotalPrice", TotalPrice);
            cmd.Parameters.AddWithValue("@CreditCardNo", TextBox2.Text);

            con1.Open();
            SqlParameter OutputParameter1 = new SqlParameter();
            OutputParameter1.ParameterName = "@CartNo";
            OutputParameter1.SqlDbType = System.Data.SqlDbType.UniqueIdentifier;
            OutputParameter1.Direction = System.Data.ParameterDirection.Output;
            cmd1.Parameters.Add(OutputParameter1);

            SqlParameter OutputParameter2 = new SqlParameter();
            OutputParameter2.ParameterName = "@PurchaseDate";
            OutputParameter2.SqlDbType = System.Data.SqlDbType.DateTime;
            OutputParameter2.Direction = System.Data.ParameterDirection.Output;
            cmd1.Parameters.Add(OutputParameter2);

            cmd1.ExecuteNonQuery();
            cmd1.Transaction.Commit();

            con1.Close();

            MessageBox("You have successfully purchased");
}

public void MessageBox(string message)
{
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageBox", "<script language='javascript'>alert('" + message + "')</script>");
}

现在我想要实现的是,在购物车网页中,如果我有2个不同玩具的2个独立推车,则存储过程&#34; PurchaseToys&#34;应该在购买按钮单击事件的表中插入2行。如果我有4个不同的玩具4个不同的玩具,存储过程&#34; PurchaseToys&#34;应该在购买按钮单击事件的表中插入4行。如果我有单个玩具的单一购物车,存储过程&#34; PurchaseToys&#34;应该在购买按钮点击事件的表格中插入单行。

所以我的问题是如何在购买按钮点击事件的表格中插入多行?

如果我的代码语法中有错误,请为我想要实现的内容显示相应的语法。

感谢。

0 个答案:

没有答案