我有一个使用MS Access 2007创建的数据库。它包含一个带有列
的表tbl_Order
Customer_Name, Dress_Type, Quantity, Date_Of_Pickup
我可以使用以下代码将记录显示到datagridview: -
Private Sub dgvCurrentOrder()
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb"
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDb.OleDbDataAdapter
da = New OleDb.OleDbDataAdapter("SELECT Customer_Name, Dress_Type, Quantity, Date_Of_Pickup " & _
"FROM tbl_order " & _
"ORDER BY Date_Of_Pickup ", con)
dt.Clear()
da.Fill(dt)
dgvReminder.DataSource = dt.DefaultView
dgvReminder.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
它显示数据库中的所有订单..但我想要的只是显示当前月份的订单..
Date_Of_Pickup
采用此格式= "28-Dec-2013"
我添加了一个`datetimepicker,并将查询修改为: -
da = New OleDb.OleDbDataAdapter("SELECT Customer_Name, Dress_Type, Quantity, Date_Of_Pickup " & _
"FROM tbl_order " & _
"WHERE Date_Of_Pickup = " & dtpCurrent.Value.Month & " " & _
"ORDER BY Date_Of_Pickup ", con)
我收到错误
标准表达式中的数据类型不匹配
我该怎么办?
答案 0 :(得分:0)
以下代码选择列Date_Of_Pickup
大于上个月最后一天的行。
Dim cmd As OleDbCommand = con.CreateCommand()
cmd.CommandText = "SELECT Customer_Name, Dress_Type, Quantity, Date_Of_Pickup " & _
"FROM tbl_order " & _
"WHERE Date_Of_Pickup > @DATE " & _
"ORDER BY Date_Of_Pickup "
Dim d As Date = dtpCurrent.Value
cmd.Parameters.AddWithValue("@DATE", New Date(d.Year, d.Month, 1, 23, 59, 59, 997).AddDays(-1.0#))
da = New OleDb.OleDbDataAdapter(cmd)
如果要提取最近30天的行,请执行以下操作:
cmd.Parameters.AddWithValue("@DATE", Date.Now.AddDays(-30.0#))