2017-04-14 71 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