我不知道这段代码的错误在哪里
I dont know where the error is in this code
尝试
MySqlConn.Open()
Dim Query = "Select * From venuesdb.cost where EventDate >= ('" & DateTimePicker1.Text & "') AND =< ('" & DateTimePicker2.Text & "')"
Command = New MySqlCommand(Query, MySqlConn)
SQLDataAdapter.SelectCommand = Command
SQLDataAdapter.Fill(DatabaseDatSet)
Bindsource.DataSource = DatabaseDatSet
DataGridView1.DataSource = Bindsource
SQLDataAdapter.Update(DatabaseDatSet)
MySqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MySqlConn.Dispose()
我一直收到错误消息,说这里有一个 SQL 错误 '>= ('" & DateTimePicker2 & " ')'
您的语法不正确,您可以使用不带 <=
的 between
和 >=
(注意运算符中的符号顺序):
Dim Query = "select *
from venuesdb.cost
where EventDate between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
或每次指定条件时指定字段:
Dim Query = "select *
from venuesdb.cost
where EventDate >= '" & DateTimePicker1.Text & "' AND EventDate <= '" & DateTimePicker2.Text & "'"
您在查询中遗漏了一个 EventDate
。将您的代码更改为:
Dim Query = "Select * From venuesdb.cost where EventDate >= ('" & DateTimePicker1.Text & "') AND EventDate =< ('" & DateTimePicker2.Text & "')"
此外,您应该在查询中使用参数以避免 SQL 注入攻击。您可以在 this SO 问题中阅读更多相关信息。
尝试
MySqlConn.Open()
Dim Query = "Select * From venuesdb.cost where EventDate >= ('" & DateTimePicker1.Text & "') AND =< ('" & DateTimePicker2.Text & "')"
Command = New MySqlCommand(Query, MySqlConn)
SQLDataAdapter.SelectCommand = Command
SQLDataAdapter.Fill(DatabaseDatSet)
Bindsource.DataSource = DatabaseDatSet
DataGridView1.DataSource = Bindsource
SQLDataAdapter.Update(DatabaseDatSet)
MySqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MySqlConn.Dispose()
我一直收到错误消息,说这里有一个 SQL 错误 '>= ('" & DateTimePicker2 & " ')'
您的语法不正确,您可以使用不带 <=
的 between
和 >=
(注意运算符中的符号顺序):
Dim Query = "select *
from venuesdb.cost
where EventDate between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
或每次指定条件时指定字段:
Dim Query = "select *
from venuesdb.cost
where EventDate >= '" & DateTimePicker1.Text & "' AND EventDate <= '" & DateTimePicker2.Text & "'"
您在查询中遗漏了一个 EventDate
。将您的代码更改为:
Dim Query = "Select * From venuesdb.cost where EventDate >= ('" & DateTimePicker1.Text & "') AND EventDate =< ('" & DateTimePicker2.Text & "')"
此外,您应该在查询中使用参数以避免 SQL 注入攻击。您可以在 this SO 问题中阅读更多相关信息。