如何使用循环从datagridview保存到sql server? (vb.net,visual studio 2015) [英] How do i save from datagridview to sql server using loop? (vb.net, visual studio 2015)

查看:287
本文介绍了如何使用循环从datagridview保存到sql server? (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆