2017-04-14 117 views
-4

我有3個表。 ItemStockDetailBranch插入多行,基於另一個表列計數

我想一次插入其中的2個。 ItemStockDetail表。

Item有3列= ItemID,Title,Price

StockDetail有3列= ItemID,BranchID,Stock

Branch has 1 column = BranchID

在下面的代碼中,插入到Item工作正常,但不是爲StockDetail表,它不插入任何東西!

現在爲StockDetail如果一切正常,我想與下面的條件將其插入:

如果添加一個項目,然後它會與所有存在BranchID加入這個項目。

這意味着,每個分支都會有這個項目。

e.g:

您添加項目,而

Branch有3排BranchID = BR000BR001BR002

它將插入到StockDetail 3行,以及,在一次(單個查詢)

StockDetail(單個查詢)的完整結果:

ItemID | BranchID | Stock 
______________________________ 
    IM000 | BR000 | 0 
    IM000 | BR001 | 0 
    IM000 | BR002 | 0 

的代碼:

'Add function' 
'Insert to StockDetail' 
Dim theCommand As New SqlCommand 
Dim theDataAdapter As New SqlDataAdapter 
Dim theDataTable As New DataTable 
theCommand.Connection = theConnection 
theCommand.CommandText = "INSERT INTO StockDetail VALUES(
         '" & Me.TextBox_ItemID.Text & "', 
         SELECT COUNT(BranchID) FROM Branch, 
         '0' 
         )" 
theDataAdapter.SelectCommand = theCommand 
'Insert to Item' 
theCommand.Connection = theConnection 
theCommand.CommandText = "INSERT INTO Item VALUES('" & Me.TextBox_ItemID.Text & "', '" & Me.TextBox_Title.Text & "', '" & Me.TextBox_Price.Text & "')" 
theDataAdapter.SelectCommand = theCommand 
theDataAdapter.Fill(theDataTable) 
DataGridView_Item.DataSource = theDataTable 
theCommand.Dispose() 
theDataAdapter.Dispose() 

更新:

下面的代碼會告訴你工作多重INSERT,但不支持BranchID INSERT。

'Insert to StockDetail' 
theConnection.Open() 
Dim theCommand As New SqlCommand 
Dim theDataAdapter As New SqlDataAdapter 
theCommand.Connection = theConnection 
theCommand.Parameters.Add("@ItemID", SqlDbType.VarChar).Value = Me.TextBox_ItemID.Text 
theCommand.CommandText = "INSERT INTO StockDetail(ItemID, BranchID, Stock) SELECT @ItemID, COUNT(Branch.BranchID), '0' FROM Branch GROUP BY Branch.BranchID" 
theDataAdapter.SelectCommand = theCommand 
Using theDataAdapter 
    theCommand.ExecuteNonQuery() 
    theCommand.Parameters.Clear() 
    theCommand.Dispose() 
    theConnection.Close() 
    SqlConnection.ClearPool(theConnection) 
End Using 

現在我該怎麼想?

不是插入1,1,。 。 。

我想用BR000BR001來插入它。 。 。 (基於所有已存在的BranchID

+4

再次......您需要參數化這些查詢。繼續構建這樣的字符串是危險的,也是邊界疏忽。 –

+2

你也應該習慣於指定你想插入的列或者你的代碼非常脆弱。當表更改時,您的查詢被破壞。 –

+0

@SeanLange wtf,我不明白參數化。我不知道btw大聲笑 – bidipeppercrap

回答

1

這裏是你如何在你的第一個插入語句中使用的參數。儘管如此,我認爲你仍然有一些非常嚴肅的邏輯問題。這將在StockDetail中插入1行,並且這些值完全沒有任何意義。您將從Branch表中插入行數作爲BranchID,這可能不是您真正想要的。我懷疑你想要的是每個分支的表格中的一行?

theCommand.CommandText = "INSERT INTO StockDetail(ItemID, BranchID, Price) VALUES(
         @ItemID, 
         (SELECT COUNT(BranchID) FROM Branch), 
         0 
         )" 
theCommand.Parameters.Add("@ItemID", SqlDbType.Varchar).Value = Me.TextBox_ItemID.Text; 

我懷疑你真正想要的是更像這樣的東西。

theCommand.CommandText = "INSERT INTO StockDetail(ItemID, BranchID, Price) 
         select @ItemID 
          , BranchID 
          , 0 
         from Branch"; 
theCommand.Parameter.Add("@ItemID", SqlDbType.Varchar).Value = Me.TextBox_ItemID.Text; 
+0

wtf'StockDetail'沒有'Title'和'Price'! 再次閱讀這個問題lol,我提到了每張表的列。 – bidipeppercrap

+0

如果你想發表你的表格作爲我們可以使用的東西,而不是猜測它不會那麼糟糕,你似乎想要一個100%完全編碼的解決方案,但不願意付出很多努力,它並不需要很大的飛躍看到這是一個打字錯誤,因爲我是憑空編寫代碼的。 –

+2

嚴重的是...停止使用wtf廢話並儘量保持專業。 –

0

插入StockDetail的SQL命令文本不會執行您所說的要發生的操作。此代碼,雖然語法不正確(如果你想使用SELECT作爲值,則需要將其包圍在括號中是這樣的:

theCommand.CommandText = "INSERT INTO StockDetail VALUES(
         '" & Me.TextBox_ItemID.Text & "', 
         (SELECT COUNT(BranchID) FROM Branch), 
         '0' 
         )" 

),將插入您的ID,分支數的計數你有,和表中的零。

對於你說你想有發生,你的代碼看起來會像這樣:

theCommand.CommandText = "INSERT INTO StockDetail SELECT 
         '" & Me.TextBox_ItemID.Text & "', 
         BranchID, '0' FROM Branch 
         )" 
+2

我不能給這個+1,因爲它是開放的sql注入。 –

+0

沒有工作:'( – bidipeppercrap

+0

爲什麼'ItemID'和'0'是從分支中取出的?您沒有讀過該分支只有1列'BranchID'。 – bidipeppercrap