Checking for an SQL result in VB.NET

后端 未结 3 586
后悔当初
后悔当初 2021-01-26 03:14

I need to check if my SQL statement returned any results from the Access db I\'m using to store my data.

I have this code atm:

        cn = New OleDbConn         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-26 04:00

    The simple answer, I guess, is this:

        Dim hasResults as Boolean = false
        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Computing Project\database.mdb;")
        cn.Open()
        cmd = New OleDbCommand("SELECT * FROM(" & roomvar.ToLower() & ") WHERE (((" & roomvar.ToLower() & ".date)=" & Chr(34) & dtpDate.Value.Date & Chr(34) & "))", cn)
        dr = cmd.ExecuteReader
            While dr.Read()
                 hasResults = true
            End While
        End If
        dr.Close()
    

    My vb-syntax is a little rusty, but you get the idea.
    But I'm not sure that this is what you actually want.

    Another variant could be

    Dim hasResults as Boolean = false
    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Computing Project\database.mdb;")
    cn.Open()
    cmd = New OleDbCommand("SELECT 1 FROM(" & roomvar.ToLower() & ") WHERE (((" & roomvar.ToLower() & ".date)=" & Chr(34) & dtpDate.Value.Date & Chr(34) & "))", cn)
    dr = cmd.ExecuteReader
        If dr.Read() Then
             hasResults = true
        End If
    End If
    dr.Close()
    

提交回复
热议问题