我制作了一个子程序来从数据库(MS Access)中读取数据并将其显示在文本框中。我使用 OleDbDataAdapter 填充 DataTable,我将使用它来显示文本框中的数据。我所做的事情是否有问题,比如没有处理应该正确处理的东西?任何评论或更改以使其看起来更好或更简单或更快速或更正确?
strConnection - 包含数据库连接字符串
strCommand - 包含 sql 命令的字符串
Public Sub readQuery(ByVal strCommand As String, ByRef dtTable As DataTable)
dtTable.Clear()
Using dbConnection As New OleDbConnection(strConnection)
dbConnection.Open()
Dim dbCommand As New OleDbCommand(strCommand, dbConnection)
Using dbReader As OleDbDataReader = dbCommand.ExecuteReader
If dbReader.HasRows Then
Dim dbAdapter As New OleDbDataAdapter(strCommand, dbConnection)
dbAdapter.Fill(dtTable)
Else
MsgBox("No data matches your search.")
End If
End Using
End Using
End Sub