如何选择datagridview中的所有行和列并保存以访问数据库 [英] how to select all rows and columns in the datagridview and save to access database

查看:86
本文介绍了如何选择datagridview中的所有行和列并保存以访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click

        DataGridView1.SelectAll()

    End Sub











//保存过程

st =INSERT INTO [学生](ID,学生,课程,Major_in,Yr_Sec,Sec,College,F_name,M_name,L_name)VALUES('& Me.DataGridView1.SelectedCells( 0).Value&','& Me.DataGridView1.SelectedCells(1).Value&','& Me.DataGridView1.SelectedCells(2).Value&','& ; Me.DataGridView1.SelectedCells(3).Val ue&','& Me.DataGridView1.SelectedCells(4).Value& ','& Me.DataGridView1.SelectedCells(5).Value& ','& Me.DataGridView1.SelectedCells(6).Value& ','& Me.DataGridView1.SelectedCells(7).Value& ','& Me.DataGridView1.SelectedCells(8).Value& ','& Me.DataGridView1.SelectedCells(9).Value& ')






//for the saving process
st = "INSERT INTO [Students](ID, Students, Course, Major_in, Yr_Sec, Sec, College, F_name, M_name, L_name )VALUES ('" & Me.DataGridView1.SelectedCells(0).Value & "','" & Me.DataGridView1.SelectedCells(1).Value & "','" & Me.DataGridView1.SelectedCells(2).Value & "','" & Me.DataGridView1.SelectedCells(3).Value & "','" & Me.DataGridView1.SelectedCells(4).Value & "','" & Me.DataGridView1.SelectedCells(5).Value & "','" & Me.DataGridView1.SelectedCells(6).Value & "','" & Me.DataGridView1.SelectedCells(7).Value & "','" & Me.DataGridView1.SelectedCells(8).Value & "','" & Me.DataGridView1.SelectedCells(9).Value & "')"

推荐答案

如果您要选择数据网格中的所有内容,为什么要选择它?只需将内容导出到Excel。



谷歌用于VB.NET sql参数化查询并忘记使用字符串连接构建SQL语句。你可以更好地抵御SQL注入攻击,你也不必处理数据中的撇号字符搞砸你的SQL语句。
If you're going to select everything in the datagrid, why select it at all? Just go to exporting the contents to Excel.

Google for "VB.NET sql parameterized queries" and forget building an SQL statement using string concatenation. You'll be much better off against SQL inject attacks and you will also not have to deal with apostrophe characters in the data screwing up your SQL statements.


只需迭代每行的INSERT。

查看以下文章以便更好地理解 -

使用C#和VB.Net将数据从DataGridView插入(保存)到Windows窗体(WinForms)应用程序中的数据库 [ ^ ]

只需要更改从SQL Server到MS Access的连接。 br />
以下文章可能对您有所帮助 -

插入,更新,删除&使用VB.NET 2005在MS Access 2003中搜索值 [ ^ ]



如果您仍然发现实施所有这些问题,请告知我们。



希望,有帮助:)
Just iterate the INSERT for each row.
Check following article for better understanding-
Insert (Save) Multiple rows from DataGridView to Database in Windows Forms (WinForms) Application using C# and VB.Net[^]
Only you have to change is the connection from SQL Server to MS Access.
Following article may help you on that regard-
Insert, Update, Delete & Search Values in MS Access 2003 with VB.NET 2005[^]

If you still find problem to implement all these, please let me know.

Hope, it helps :)


这篇关于如何选择datagridview中的所有行和列并保存以访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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