将数据添加到数据库中(asp.net)

时间:2016-03-17 23:20:45

标签: c# sql asp.net sql-server tsql

我有几页预订,每页保存数据。例如,第一页将目的地添加到数据库,第二页是选择乘客数量。

我有一张表来存储所有这些:

CREATE TABLE [dbo].[Transactions] (
[cardNumber ] NCHAR (10) NULL,
[Cost]        NCHAR (10) NULL,
[Passengers]  NCHAR (10) NULL,
[Destination] NCHAR (10) NULL
);

在目标页面上,我使用以下代码将目标输入到数据库:

protected void Button2_Click1(object sender, EventArgs e)
{
    try
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);

        conn.Open();
        string insert = "insert into Transactions (Destination) values (@Destination)";

        SqlCommand com = new SqlCommand(insert, conn);

        com.Parameters.AddWithValue("@Destination", DropDownList1.SelectedItem);

        com.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception ex)
    {
        Response.Write("Error: " + ex.ToString()); 
    }

    Response.Redirect("Booking.aspx"); 
}

在下一页我输入的乘客数量相对相同:

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);

        conn.Open();

        string insert = "insert into Transactions (Passengers) values (@Passengers)";

        SqlCommand com = new SqlCommand(insert, conn);
        com.Parameters.AddWithValue("@Passengers", DropDownList1.SelectedItem);

        com.ExecuteNonQuery();
        conn.Close();
    }
    catch(Exception ex)
    {
        Response.Write("Error: " + ex.ToString());
    }

    Response.Redirect("Payment.aspx");

}

但这样做之后,没有数据输入数据库。如果有人知道我可以一次将数据输入数据库,请告诉我。

如果不能以这种方式完成,并且有更好的方法再次这样做,请告诉我。

谢谢大家的时间。

1 个答案:

答案 0 :(得分:0)

您的桌子上应该有一个专用的主键列,我建议使用自动增量整数。

CREATE TABLE [dbo].[Transactions]
(
    [ID]          INT NOT NULL PRIMARY KEY IDENTITY(1,1),
    [CardNumber]  NCHAR (10) NULL,
    [Cost]        NCHAR (10) NULL,
    [Passengers]  NCHAR (10) NULL,
    [Destination] NCHAR (10) NULL
);

然后,使用存储过程,而不是ad-hoc SQL。

CREATE PROCEDURE TransactionSave
(
    @ID int = null,
    @CardNumber nchar(10) = null,
    @Cost nchar(10) = null,
    @Passengers nchar(10) = null,
    @Destination nchar(10) = null
)
AS
BEGIN

DECLARE @ExistingID int
SELECT @ExistingID = ID FROM Transaction WHERE ID = @ID

IF @ExistingID is null
BEGIN
     --Insert
     INSERT INTO Transaction (CardNumber, Cost, Passengers, Destination)
     VALUES (@CardNumber, @Cost, @Passengers, @Destination)

     SELECT CAST(SCOPE_IDENTITY() AS INT) AS 'TransactionID'
END
ELSE
BEGIN
     --Update
     UPDATE Transaction
     SET
         CardNumber = ISNULL(@CardNumber, CardNumber),
         Cost = ISNULL(@Cost, Cost),
         Passengers = ISNULL(@Passengers, Passengers),
         Destination = ISNULL(@Destination, Destination),
     WHERE ID = @ExistingID

     SELECT @ExistingID AS 'TransactionID'
END

END

然后,在您的代码中,您需要保留您正在处理的事务的ID值,以确保您正在更新正确的行:

protected void Button2_Click(object sender, EventArgs e)
{
    int transactionID = hfID.Value;

    try
    {
        using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("TransactionSave", conn);

            cmd.Parameters.AddWithValue("@ID", transactionID);
            cmd.Parameters.AddWithValue("@Passengers", DropDownList1.SelectedValue);

            transactionID = cmd.ExecuteScalar();
            hfID.Value = transactionID;
        }
    }
    catch(Exception ex)
    {
        Response.Write("Error: " + ex.ToString());
    }
}