简体   繁体   English

vb.net IsDBNull如果语句未显示msgbox

[英]vb.net IsDBNull if statement not displaying msgbox

I am trying to check for empty records in an access db and thought that the code I am using would work. 我试图检查访问数据库中的空记录,并认为我正在使用的代码可以工作。 What is supposed to happen, is if there are no records for that table in the db, then the msgbox is displayed. 应该发生的是,如果数据库中没有该表的记录,则显示msgbox。 However, nothing is displaying when I run the code. 但是,当我运行代码时,什么都没有显示。 Am I using IsDBNull the correct way or is there a better way to do it. 我是正确使用IsDBNull还是有更好的方法来做到这一点? I am getting to grips with using params instead of & references and this shall be changed after testing. 我开始使用params代替&引用,并且在测试后应对此进行更改。 Many thanks. 非常感谢。

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

Yes, you are using IsDBNull completely wrong. 是的,您使用IsDBNull完全错误。 The documentation for that method states: 该方法的文档指出:

Returns a Boolean value that indicates whether an expression evaluates to the System.DBNull class. 返回一个布尔值,该布尔值指示表达式是否对System.DBNull类求值。

The sql variable you're sending into the IsDBNull method clearly does not evaluate to System.DBNull , as you just set it to some other value yourself. 您发送到IsDBNull方法中的sql变量显然不会评估为System.DBNull ,因为您自己将其设置为其他值。

You use IsDBNull after you've executed your SQL, to check to see if a specific field in your result was NULL. 执行IsDBNull SQL后,可以使用IsDBNull来检查结果中的特定字段是否为NULL。 To do what you want, you'll just want to check if the data table has any rows it in after you call Fill . 要执行所需的操作,只需在调用Fill之后检查数据表中是否包含任何行。

Dim dt As New DataTable("Departments")
adapter.Fill(dt)
If dt.Rows.Count = 0 Then MessageBox.Show("No record")

IsDbNull will not call your database. IsDbNull将不会调用您的数据库。 It just tests if the parameter is equal to DbNull.Value which will never be the case if you call it with a string. 它只是测试参数是否等于DbNull.Value,如果您使用字符串调用它,则永远不会如此。

you could try this 你可以试试这个

Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\test.accdb")
Dim sql As String
sql = "SELECT COUNT(*) FROM Departments where Customer = '" & customer & "'"
con1.Open()
Dim cmd1 As New OleDbCommand(sql, con1)
If cmd1.ExecuteScalar() = 0 Then
  MessageBox.Show("No record")
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)
  cmd1 = New OleDbCommand(sql1, con1)
  'Dim dt1 As New DataTable("Departments")
  Dim myreader As OleDbDataReader = cmd1.ExecuteReader
  myreader.Read()

  con1.Close()
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM