Checking for an SQL result in VB.NET

后端 未结 3 613
后悔当初
后悔当初 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 03:59

    Using cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Computing Project\database.mdb;")
        cn.Open()
        Using cmd As New OleDbCommand(String.Format("SELECT * FROM({0}) WHERE ((({0}.date)=""{1}""))", roomvar.ToLower(), dtpDate.Value.Date), cn)
            Using dr As OleDbDataReader = cmd.ExecuteReader()
                hasResults = dr.Read()
            End Using
        End Using
    End Using
    

    I strongly suggest you look at passing the dtpDate value as a parameter instead of a literal value in the command text, e.g.:

    Using cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Computing Project\database.mdb;")
        cn.Open()
        Using cmd As New OleDbCommand(String.Format("SELECT * FROM({0}) WHERE ((({0}.date)=?))", roomvar.ToLower()), cn)
            cmd.Parameters.Add("@dtpDate", OleDbType.Date).Value = dtpDate.Value.Date
            Using dr As OleDbDataReader = cmd.ExecuteReader()
                hasResults = dr.Read()
            End Using
        End Using
    End Using
    

提交回复
热议问题