Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
If R2.Checked Then
da = New OleDbDataAdapter("Select * from [maint] where
[centrale]='" & centralev.Text & "' ", MyConn) 'Change items to your
database name
End If
If R1.Checked Then
da = New OleDbDataAdapter("Select * from [maint] where
datevisite='" & dateintervention.Value.ToShortDateString() & "' ",
MyConn) 'Change items to your database name
End If
da.Fill(ds, "maint") 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view
DGV.DataSource = view
End Sub
End Class
我使用2 buttonradio来选择搜索条件,我有日期搜索问题,条件表达式中的数据类型不匹配。
答案 0 :(得分:0)
尝试改变
不正确
da = New OleDbDataAdapter("Select * from [maint] where
datevisite='" & dateintervention.Value.ToShortDateString() & "' ",
MyConn) 'Change items to your database name
正确
da = New OleDbDataAdapter("Select * from [maint] where
datevisite='" & dateintervention.Value.ToString("yyyy-MM-dd") & "' ",
MyConn) 'Change items to your database name
答案 1 :(得分:0)
扩展Plutonix对您的问题的评论,您可以尝试:
Dim cmd = New OleDbCommand("Select * from [maint] where datevisite = ?")
Dim param = New OleDbParameter("@datevisite", OleDbType.Date)
param.Value = dateintervention.Value
cmd.Parameters.Add(param)
da = New OleDbDataAdapter (cmd, MyConn)
通过这种方式,ADO.NET将照顾您使用OleDb语法所需格式化的正确日期值替换查询文本中的问号。您完全避免了字符串连接错误,并且您不需要学习所有可用OleDb数据类型的任何格式规则。
此外它应该更安全。