这是我以前写的一个基于VB的SQL条件查询。你参考一下。
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim s As String
Dim wendu As String
Dim home As room
Dim time As String
wendu = TextBox6.Text.ToString
Try
con.ConnectionString = "server = USER-20150216TC\ZHOU;database = ZHOU;user id = sa;password=zhou;"
s = "select room1,room2,co,red, tim from Table_1 " + "where room1>=" + "'" + wendu.ToString + "'" + "or room2>=" + "'" + wendu.ToString + "'"
cmd.Connection = con
= CommandType.Text
= s
con.Open()
dr = cmd.ExecuteReader
Do While dr.Read()
home.wendu1 = dr.GetString(0)
home.wendu2 = dr.GetString(1)
home.co = dr.GetString(2)
home.red = dr.GetString(3)
time = dr.GetString(4)
DataGridView1.Rows.Add(home.wendu1, home.wendu2, home.co, home.red, time)
Loop
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim s As String
Dim wendu As String
Dim home As room
Dim time As String
wendu = TextBox6.Text.ToString
Try
con.ConnectionString = "server = USER-20150216TC\ZHOU;database = ZHOU;user id = sa;password=zhou;"
s = "select room1,room2,co,red, tim from Table_1 " + "where room1>=" + "'" + wendu.ToString + "'" + "or room2>=" + "'" + wendu.ToString + "'"
cmd.Connection = con
= CommandType.Text
= s
con.Open()
dr = cmd.ExecuteReader
Do While dr.Read()
home.wendu1 = dr.GetString(0)
home.wendu2 = dr.GetString(1)
home.co = dr.GetString(2)
home.red = dr.GetString(3)
time = dr.GetString(4)
DataGridView1.Rows.Add(home.wendu1, home.wendu2, home.co, home.red, time)
Loop
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub