如何使用循环从datagridview保存到sql server? (vb.net,visual studio 2015) [英] How do i save from datagridview to sql server using loop? (vb.net, visual studio 2015)
问题描述
我似乎无法将多个数据从我的datagridview保存到SQL Server。
当我使用messagebox循环时,从第1行到第10行的循环起作用。但是当使用相同的代码插入数据时,它只能保存1行然后代码退出。
任何感觉都愿意提供帮助吗?
:(
我尝试了什么:
Dim c As整数= 0
Dim dgv As Integer = DataGridView1.Rows.Count - 1
而c< dgv
ExecuteSQLQuery(INSERT INTO G_Masterlist (serial_no)VALUES(+ DataGridView1.Item(3,c).Value.ToString +);)
'MsgBox(DataGridView1.Item(3,c).Value.ToString)
c + = 1
结束时
也试过这个
For Data band As DataGridViewBand In DataGridView1.Rows
Dim strItemID As String = CStr(DataGridView1.Rows(band.Index).Cells(3).Value)
'MsgBox(strItemID)
sqlSTR =INSERT INTO G_Masterlis t(serial_no)VALUES('+ strItemID +')
ExecuteSQLQuery(sqlSTR)
下一步
使用正确的数据绑定,框架将为您完成大部分工作;请参阅更新,插入和删除数据集中的记录 [< a href =https://msdn.microsoft.com/en-us/library/aa984437(v=vs.71).aspx\"target =_ blanktitle =New Window> ^ ]。
I cant seem to save multiple data from my datagridview to SQL Server.
When i loop using messagebox, the loop from line 1 to 10 works. But when using the same code to insert data, it can only save 1 line then the code exits.
Any sensei willing to help?
:(
What I have tried:
Dim c As Integer = 0
Dim dgv As Integer = DataGridView1.Rows.Count - 1
While c < dgv
ExecuteSQLQuery("INSERT INTO G_Masterlist (serial_no) VALUES (" + DataGridView1.Item(3, c).Value.ToString + ");")
'MsgBox(DataGridView1.Item(3, c).Value.ToString)
c += 1
End While
"also tried this"
For Each band As DataGridViewBand In DataGridView1.Rows
Dim strItemID As String = CStr(DataGridView1.Rows(band.Index).Cells(3).Value)
'MsgBox(strItemID)
sqlSTR = "INSERT INTO G_Masterlist (serial_no) VALUES ('" + strItemID + "')"
ExecuteSQLQuery(sqlSTR)
Next
Use proper data binding and the framework will do most of the work for you; see Updating, Inserting, and Deleting Records in a Dataset[^].
这篇关于如何使用循环从datagridview保存到sql server? (vb.net,visual studio 2015)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!