2012-05-13 49 views
0

我正在爲我的項目開發購物車,並且在如何檢索orderid並插入另一個表時遇到問題。同時檢索並插入一個表的值到另一個表

我在SQL Server中的兩個表

Order 
orderid (PK) | date| orderstatus| name| shippingaddress| contactnumber| totalprice| customerid| 

Order_book 
orderid (PK) | ISBN (PK)| quantity 

我想從順序表中檢索訂單ID,並將其插入order_book,每當訂單提交到數據庫中,訂單ID以表身份INT與增量1.

什麼是最好的選擇來檢索和插入在同一時間從一個表到另一個使用c#?

謝謝。

代碼::

protected void checkout_Click(object sender, EventArgs e) 
{ 
    SQLInjection sql = new SQLInjection(); 
    String name; 
    String address; 
    int contactnumber; 
    if (newaddresspanel.Visible == true) 
    { 
     name = sql.SafeSqlLiteral(NameLabel.Text, 2); 
     address = Address1.Text + " " + Address2.Text + "," + PostalCode.Text + "," + State.SelectedItem.Value; 
     contactnumber = int.Parse(Telephone1.SelectedItem.Value) + int.Parse(Telephone2.Text); 
     insertDetails(name, address, contactnumber); 
    } 
    else if (defaultaddresspanel.Visible) 
    { 
     SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookWormDBConnectionString"].ConnectionString);//connect to database 
     SqlCommand bindDetails = new SqlCommand("SELECT NAME, ADDRESS, TELEPHONE_NUMBER FROM CUSTOMER WHERE CUSTOMERID = @username", myConnection); 

     bindDetails.Parameters.Add("@username", SqlDbType.VarChar); 
     bindDetails.Parameters["@username"].Value = username; 

     try 
     { 
      myConnection.Open(); 
      SqlDataReader reader = bindDetails.ExecuteReader(); 
      while (reader.Read()) 
      { 
       name = reader["NAME"].ToString(); 
       address = reader["ADDRESS"].ToString(); 
       contactnumber = int.Parse(reader["TELEPHONE_NUMBER"].ToString()); 
       insertDetails(name, address, contactnumber); 

       } 
      reader.Dispose(); 
      myConnection.Close(); 
     } 
     catch (SqlException se) 
     { 


     } 
    } 
    try 
    { 
     cart = (DataTable)HttpContext.Current.Session["Cart"]; 
     foreach (DataRow dr in cart.Rows) 
     { 
      int isbn = int.Parse(dr["ISBN"].ToString()); 
      int quantity = int.Parse(dr["quantity"].ToString()); 
      insertOrderbook(isbn, quantity); 
     } 


    } 

    catch (Exception ae) 
    { 

     Response.Write(ae.Message); 

    } 

插入手持訂單: 「哪個失敗」

protected void insertOrderbook(int isbn, int quantity) 
{ 

    int orderid; 
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookWormDBConnectionString"].ConnectionString);//connect to database 
    SqlCommand orderId = new SqlCommand("SELECT SCOPE_IDENTITY() FROM ORDERBOOK", myConnection); 


    try 
    { 
     myConnection.Open(); 

     orderid = int.Parse(orderId.ExecuteScalar().ToString()); 
      SqlCommand insertOrderBook = new SqlCommand("INSERT INTO ORDERBOOK_BOOK (ISBN, ORDERID, QUANTITY) VALUES (@isbn, @orderid, @quantity)", myConnection); 

      insertOrderBook.Parameters.Add("@isbn", SqlDbType.Int); 
      insertOrderBook.Parameters["@isbn"].Value = isbn; 

      insertOrderBook.Parameters.Add("@orderid", SqlDbType.Int); 
      insertOrderBook.Parameters["@orderid"].Value = orderid; 

      insertOrderBook.Parameters.Add("@quantity", SqlDbType.Int); 
      insertOrderBook.Parameters["@quantity"].Value = quantity; 

      insertOrderBook.ExecuteNonQuery(); 
      myConnection.Close(); 
     } 
    catch (SqlException se) 
    { 
     Response.Write(se.Message); 
    } 
} 

插入訂單的詳細信息::

protected void insertDetails(string name, string address, int contactnumber) 
{ 
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BookWormDBConnectionString"].ConnectionString);//connect to database 
    SqlCommand orderDetails = new SqlCommand("INSERT INTO ORDERBOOK (DATE, ORDERSTATUS, TOTALPRICE, SHIPPING_ADD, CUSTOMERID, NAME, CONTACTNUMBER) VALUES (@date, @orderstatus, @totalprice, @shippingadd, @username, @name, @contactnumber)", myConnection); 
    orderDetails.Parameters.Add("@date", SqlDbType.DateTime); 
    orderDetails.Parameters["@date"].Value = getTime(); 

    orderDetails.Parameters.Add("@orderstatus", SqlDbType.VarChar); 
    orderDetails.Parameters["@orderstatus"].Value = "Order Processing"; 

    orderDetails.Parameters.Add("@totalprice", SqlDbType.Decimal); 
    orderDetails.Parameters["@totalprice"].Value = totalPrice; 

    orderDetails.Parameters.Add("@shippingadd", SqlDbType.VarChar); 
    orderDetails.Parameters["@shippingadd"].Value = address; 

    orderDetails.Parameters.Add("@name", SqlDbType.VarChar); 
    orderDetails.Parameters["@name"].Value = name; 

    orderDetails.Parameters.Add("@contactnumber", SqlDbType.Int); 
    orderDetails.Parameters["@contactnumber"].Value = contactnumber; 

    orderDetails.Parameters.Add("@username", SqlDbType.VarChar); 
    orderDetails.Parameters["@username"].Value = username; 

    try { 
     myConnection.Open(); 
     orderDetails.ExecuteNonQuery(); 
     myConnection.Close(); 

    } 
    catch (SqlException se) 
    { 


    } 
} 

錯誤:輸入字符串的格式不正確

我試圖調試它,不知何故,orderid顯示0

+0

當前訂單如何提交給數據庫? –

+0

我現在正在使用「從訂單where where customerid = username,date = date」和「insert into order_book(isbn,quantity,orderid)values isbn,quantity orderid」中選擇orderid。 這發生在同一時間,但不知何故有一個錯誤。 對不起,我是一個編程新手。謝謝 – eugene

+1

請發佈您使用的代碼以及您收到的錯誤消息 –

回答

0

你正在做這個選擇

SELECT SCOPE_IDENTITY() FROM ORDERBOOK 

你應該做的是一個輸出參數(讓我們說,@orderBookId)添加到您的訂單插入SP,並在其上,插入,你應該右後做

SELECT @orderBookId = SCOPE_IDENTITY() 

注意沒有FROM子句,並且一定要在您的C#代碼中添加參數作爲輸出參數。

然後,插入細節時,只需添加@orderBookId作爲輸入參數即可。

1

如果我理解你正確,你想檢索新創建的orderId,當你插入一個訂單,然後把這個ID在order_book。

要檢索上次創建的值,請在將項目插入訂單表後立即使用SELECT SCOPE_IDENTITY()。這會給你orderId插入到order_book。

相關問題