我正在尝试检查访问数据库中的空记录,并认为我使用的代码可以工作。应该发生的是,如果数据库中没有该表的记录,则显示 msgbox。但是,当我运行代码时,什么都没有显示。我是以正确的方式使用 IsDBNull 还是有更好的方法。我开始掌握使用参数而不是 & 引用,这将在测试后更改。非常感谢。
Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\test.accdb")
Dim sql As String
sql = "SELECT * FROM Departments where Customer = '" & customer & "'"
If IsDBNull(sql) Then
MessageBox.Show("No record") <---THIS NOT FIRING
' This is our DataAdapter. This executes our SQL Statement above against the Database
' we defined in the Connection String
Else
Dim adapter As New OleDbDataAdapter(sql, con1)
' Gets the records from the table and fills our adapter with those.
Dim dt As New DataTable("Departments")
adapter.Fill(dt)
' Assigns our DataSource on the DataGridView
dgv1.DataSource = dt
'
Dim sql1 As String
sql1 = "SELECT * FROM Departments"
Dim adapter1 As New OleDbDataAdapter(sql1, con1)
Dim cmd1 As New OleDbCommand(sql1, con1)
'Dim dt1 As New DataTable("Departments")
con1.Open()
Dim myreader As OleDbDataReader = cmd1.ExecuteReader
myreader.Read()
con1.Close()
End If