2013-04-18 31 views
1

我有一個用VB.NET編寫的ASP.NET頁面,通過INNER JOIN使用SELECT語句將項目獲取到GridView,並且還允許您將項目添加到發票。SQL_INSERT和Scope_Identity() - 獲取記錄ID

INNER JOINitemsproject_items獲得數據。

SELECT Items.item_id, Items.item_name, Items.item_cost, project_items.item_quantity 
FROM Items 
INNER JOIN project_items 
ON items.item_id = project_items.item_id 
WHERE project_items.project_id = @parameter 

@parameter isSession("ProjectID")

(有一個外鍵project_items.item_id -> items.item_id。)

我有一個試圖用SQL語句在VB.NET,試圖同時插入到兩個表。我試過是我試圖讓創造的最後一個記錄的item_id並通過使用數據插入到另一個表(project_items)。但是,數據僅被輸入到第一個表格中。

任何想法我可以做什麼?

這是代碼:

Protected Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click 

     Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True") 
     Dim addItemComm As String = "SELECT item_id FROM project_items WHERE [email protected]" 
     Dim user_id_select As New Integer 

     Dim addItemSQL As New SqlCommand 

     conn.Open() 

     addItemSQL = New SqlCommand(addItemComm, conn) 
     addItemSQL.Parameters.AddWithValue("@ProjectID", Convert.ToInt32(Session("ProjectID"))) 


     Dim datareader As SqlDataReader = addItemSQL.ExecuteReader() 


     datareader.Close() 
     conn.Close() 

     Dim AddNewItemComm As String = "INSERT INTO Items (item_name, item_cost, item_code) VALUES (@ItemName, @ItemCost, @ItemCode); SELECT SCOPE_IDENTITY()" 
     Dim AddNewItem2Comm As String = "INSERT INTO project_items (item_id, project_id, item_quantity) VALUES (@ItemID, @ProjectID, @ItemQuantity) " 
     Dim AddNewItemSQL As New SqlCommand 


     conn.Open() 

     AddNewItemSQL = New SqlCommand(AddNewItemComm, conn) 
     AddNewItemSQL.Parameters.AddWithValue("@ItemName", txtItemName.Text.Trim) 
     AddNewItemSQL.Parameters.AddWithValue("@ItemCost", Convert.ToInt32(txtItemCost.Text)) 
     AddNewItemSQL.Parameters.AddWithValue("@ItemCode", txtItemCost.Text.ToString.ToUpper) 

     Dim ItemId As Integer 

     ItemId = AddNewItemSQL.ExecuteScalar() 

     AddNewItemSQL.ExecuteNonQuery() 

     conn.Close() 

     conn.Open() 

     AddNewItemSQL = New SqlCommand(AddNewItem2Comm, conn) 

     AddNewItemSQL.Parameters.AddWithValue("@ItemID", ItemId) 
     AddNewItemSQL.Parameters.AddWithValue("@ProjectID", Convert.ToInt32(Session("ProjectID"))) 
     AddNewItemSQL.Parameters.AddWithValue("@ItemQuantity", Convert.ToInt32(txtItemQuantity.Text)) 

     AddNewItemSQL.ExecuteNonQuery() 

     conn.Close() 



    End Sub 
+0

你得到任何錯誤??? – mmpatel009

+0

只是用於調試目的的try/catch語句添加到上面的代碼,並檢查是否有任何異常。另外爲什麼關閉/打開兩個命令之間的連接? – Steve

+0

你在調試中一行一行地運行它嗎?你是否從第一個命令中獲得預期的ItemID? –

回答

3

你爲什麼要在多條語句首先這樣做呢?爲什麼不:

INSERT dbo.Items (item_name, item_cost, item_code) 
    OUTPUT inserted.ItemID, @ProjectID, @ItemQuantity 
    INTO dbo.project_items(item_id, project_id, item_quantity) 
VALUES (@ItemName, @ItemCost, @ItemCode); 

現在您只需撥打一個ExecuteNonQuery()和您的應用程序不必關心所產生的實際SCOPE_IDENTITY()值。 (您仍然可以檢索SCOPE_IDENTITY()如果你願意,當然,使用ExecuteScalar - 但內納德正確地指出,選擇一個而不是調用兩個。)

因爲我們現在知道,有一個明確的外鍵在這裏,我們可以即使我們不能使用OUTPUT子句,仍然會將您的C#代碼減少到一個呼叫。

DECLARE @i INT; 

INSERT dbo.Items (item_name, item_cost, item_code) 
    SELECT @ItemName, @ItemCost, @ItemCode; 

SELECT @i = SCOPE_IDENTITY(); 

INSERT dbo.project_items(item_id, project_id, item_quantity) 
    SELECT @i, @ProjectID, @ItemQuantity 

SELECT @i; -- if necessary 

將它放到存儲過程中會更清晰。

+0

出現此錯誤:'OUTPUT INTO子句的目標表'dbo.project_items'不能位於(主鍵,外鍵)關係的任一側。找到參考約束「FK__project_i__item ___ 1CBC4616'.' – Brian

0
ItemId = AddNewItemSQL.ExecuteScalar() 

     AddNewItemSQL.ExecuteNonQuery() 

這兩排彼此相鄰兩次將執行該命令。你應該刪除第二個 - ExecuteNonQuery。這將使您的數據在Items中插入兩次 - 兩個相同的行,但具有不同的ID。

由於您只從第一行檢索到ItemID,應該在project_items中插入該ItemID,但上次插入到項目中的另一個將沒有匹配的行。

而且 - 從按鈕單擊方法了年初完成部分Dim AddNewItemComm As String之前 - 在打開和關閉的DataReader,什麼也不做它似乎完全沒有必要的。