我的问题是在日期时间选择器中选择的两个日期之间使用mysql搜索vbnet。我在很多论坛中搜索过它但很遗憾没有运气
Dim dbdataset As New DataTable
Dim bsource As New BindingSource
Dim sda As New MySqlDataAdapter
Dim reader As MySqlDataReader
Try
mysqlconn.Open()
' MessageBox.Show("ok")
Dim query As String
query = "SELECT * FROM(patient) WHERE imerominia BETWEEN '" & DateTimePicker3.Text & "' AND '" & DateTimePicker4.Text & "' group BY imerominia"
command = New MySqlCommand(query, mysqlconn)
sda.SelectCommand = command
sda.Fill(dbdataset)
bsource.DataSource = dbdataset
DataGridView1.DataSource = bsource
sda.Update(dbdataset)
reader = command.ExecuteReader
While reader.Read
Dim imerominia As String = reader.GetString("imerominia")
Dim imerominia1 As String = reader.GetString("imerominia")
DateTimePicker3.Text = (imerominia)
DateTimePicker4.Text = (imerominia1)
End While
mysqlconn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
mysqlconn.Dispose()
End Try
End Sub
答案 0 :(得分:1)
Dim dbdataset As New DataTable
Dim bsource As New BindingSource
Dim sda As New MySqlDataAdapter
Dim reader As MySqlDataReader
Try
mysqlconn.Open()
' MessageBox.Show("ok")
'Dim query As String
'query = "SELECT * FROM patient WHERE imerominia BETWEEN '" & DateTimePicker3.Text & "' AND '" & DateTimePicker4.Text & "' group BY imerominia"
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "SELECT * FROM patient WHERE imerominia BETWEEN '" & @p_Date & "' AND '" & @p1_Date & "' group BY imerominia"
cmd.Parameters.Add ("@p_Date", SqlDbType.DateTime).Value = DateTimePicker3.Value;
cmd.Parameters.Add ("@p1_Date", SqlDbType.DateTime).Value = DateTimePicker4.Value;
sda.SelectCommand = cmd
sda.Fill(dbdataset)
bsource.DataSource = dbdataset
DataGridView1.DataSource = bsource
sda.Update(dbdataset)
reader = cmd.ExecuteReader
While reader.Read
Dim imerominia As String = reader.GetString("imerominia")
Dim imerominia1 As String = reader.GetString("imerominia")
DateTimePicker3.Text = (imerominia)
DateTimePicker4.Text = (imerominia1)
End While
mysqlconn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
mysqlconn.Dispose()
End Try
End Sub
不应使用 DateTimePicker3.Text
,而是可以使用DateTimePicker3.Value.Date
。 DateTimePicker.Value
是控件表示的DateTime。 DateTimePicker.Text
是控件如何根据Format
属性