VB.NET 将数据表行插入 SQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13173494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Insert datatable rows into SQL Database
提问by user1699905
I have the following code to loop through my datatable (dtItem) and insert each row into the database. However, i can only insert the last row of the datatable. How do i insert all rows? Here is my code.
我有以下代码循环遍历我的数据表 (dtItem) 并将每一行插入到数据库中。但是,我只能插入数据表的最后一行。如何插入所有行?这是我的代码。
Dim dtRow As DataRow
Dim dtRow 作为 DataRow
For Each dtRow In dtItem.Rows
dtRow.ToString.Split("|")
Dim xBinCode As String = dtRow(0)
Dim xLocationCode As String = dtRow(7)
Dim xItemNo As String = dtRow(1)
Dim xQuantity As String = dtRow(2)
Dim xCountNo As String = dtRow(8)
cmd.CommandText = "INSERT INTO tblItems (BinCode, LocationCode, ItemNo, Quantity, CountNo) values('" & xBinCode & "','" & xLocationCode & "','" & xItemNo & "','" & xQuantity & "','" & xCountNo & "')"
Next
回答by Maadh
if you use data reader you need to open connection and use command
如果您使用数据阅读器,则需要打开连接并使用命令
cmd.ExecuteNonQuery
after
后
cmd.CommandText = "INSERT INTO tblItems (BinCode, LocationCode, ItemNo, Quantity, CountNo) values('" & xBinCode & "','" & xLocationCode & "','" & xItemNo & "','" & xQuantity & "','" & xCountNo & "')"
statement
陈述
回答by Alex Pollan
It looks like you're missing something here.
看起来你在这里遗漏了一些东西。
You can do that concatening the SQL insert sentences for each row before sending to server.
您可以在发送到服务器之前连接每一行的 SQL 插入语句。
If you want to send one row at time, configure and execute your command inside the for loop.
如果您想一次发送一行,请在 for 循环内配置并执行您的命令。
You can also use a DbDataAdapter. Loak at the following pages:
您还可以使用 DbDataAdapter。查看以下页面:
http://msdn.microsoft.com/es-es/library/at8a576f(v=vs.90).aspx
http://msdn.microsoft.com/es-es/library/at8a576f(v=vs.90).aspx

