比如说,我有一个从 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 一无所知?