使用 VB.NET 将 datagridview 中的多条记录插入到我的数据库表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16031060/
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
Insert multiple record from datagridview into my database table useing VB.NET
提问by Vinra Gunanta Pandia
i want to insertrecords from datagridviewto mysql database table,
but i dont know how to insert datagridviewto database table.
How to do it? :(
我想insert记录从datagridview到 mysql database table,但我不知道如何插入 datagridview到数据库table。怎么做?:(
采纳答案by sentil kumar
This is jus my suggestion if you have you code then can work out more better
这只是我的建议,如果你有你的代码然后可以更好地工作
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
''create connection
Dim conn As SqlConnection = New SqlConnection()
conn.Open()
Dim comm As SqlCommand = New SqlCommand()
comm.Connection = conn
''insert data to sql database row by row
Dim name, ageAs String
For i As Integer = 0 To Me.DataGridView1.Rows.Count
name = Me.DataGridView1.Rows(i).Cells(0).ToString()
age= Me.DataGridView1.Rows(i).Cells(1).ToString()
comm.CommandText = "insert into mytable(name,age) values('" & name & "','" & age& "')"
comm.ExecuteNonQuery()
Next
conn.Close()
End Sub
This is just a way to insert it is best it you use stored procedure to insert the value because if you use SQL it might happen SQL Injection. So stored procedure is the best way and save to use.
这只是一种插入方式,最好使用存储过程插入值,因为如果使用 SQL 可能会发生 SQL 注入。所以存储过程是最好的方式并保存使用。
回答by Exray Mustie-phizzle Madubuike
kumar, there is a problem with your code
kumar,你的代码有问题
name = Me.DataGridView1.Rows(i).Cells(0).ToString()
age= Me.DataGridView1.Rows(i).Cells(1).ToString()
SHOULD BE
应该
name = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
age= Me.DataGridView1.Rows(i).Cells(1).Value.ToString()
回答by Sotiris
Change the line of your code:
更改代码行:
For i As Integer = 0 To Me.AddPurchases.Rows.Count
with this line:
用这一行:
For i As Integer = 0 To Me.AddPurchases.Rows.Count - 1
The above is the solution for the exception of verohwm's message. *
以上是针对verohwm的消息异常的解决方法。*
回答by Umesh Kaushik
instead of inserting data I was trying to update the record and for me
而不是插入数据我试图更新记录和我
Me.AddPurchases.Rows(i).Cells(0).Value.ToString()
Me.AddPurchases.Rows(i).Cells(0).Value.ToString()
this statement did not solve the purpose this statement will return the row count and column count like { row=0,col=1} next iteration { row=0,col=1} and so on but
这个语句没有解决目的这个语句将返回行数和列数,如 { row=0,col=1} nextiteration { row=0,col=1} 等等但是
Dim roid As String
For i As Integer = 0 To Me.edit_role_grid.RowCount - 1
roid = Me.edit_role_grid.Rows(i).Cells(0).FormattedValue
MsgBox(roid)
this worked fine for me, this is returning the text of the cell and now this value can be used anywhere
这对我来说很好用,这是返回单元格的文本,现在这个值可以在任何地方使用

