说,我有一个从SQL表中获取数据的应用程序:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable
Dim QRY As String
QRY = "SELECT * FROM Table1"
dt = GetData(QRY)
DataGridView1.DataSource = dt
QRY = "SELECT * FROM Table2"
dt = GetData(QRY)
DataGridView2.DataSource = dt
End Sub
Public Function GetData(ByVal QRY As String) As DataTable
Dim dt As New DataTable
Dim con As New SqlConnection(connection string here)
Dim adapter As New SqlDataAdapter(QRY, con)
Try
con.Open()
adapter.Fill(dt)
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return dt
End Function
End Class
如果DataGridView 1和2已更改(添加了行,已删除的行,已编辑的单元格),我不知道如何通过adapter.update更新表1和2。 或者这是不可能的,adapter.update对通过我的函数填充的DataGridView一无所知?