0

我正在使用 adatagridview来显示学生记录..

我想显示按房东出生日期过滤的记录。

datetimepicker当我通过使用自定义格式从 a 中选择 11/09 来搜索数据时dd/mm,它什么也没显示。

如何通过仅匹配 dd/mm 而忽略年份来显示所有记录?

这是我的代码:

Try
    con = New OleDbConnection(cs)
    con.Open()

    cmd = New OleDbCommand("SELECT HostelerID as [Hosteler ID],HostelerName as [Hosteler Name],DOB,HostelName as [Hostel Name],RoomNo as [Room No],DateOfJoining as [Date Of Joining],Purpose,FatherName as [Father's Name],MobNo1 as [Mobile No],Phone1 as [Phone No],MotherName as [Mother's Name],MobNo2 as [Mobile No 2],City,Address,Email,ContactNo as [Contact No],InstOfcDetails as [Ins/Ofc Details],Phone2 as [Phone No 2],Agreement,GuardianName as  [Guardian Name],GuardianAddress as [Guardian Address],MobNo3 as [Guardian Mobile No],Phone3 as [Guardian Phone No],FixedDeposit as [Fixed Deposit],CompletionDate as [Completion Date],Photo,DocsPic as [Docs Pic] from Hostelers where DOB like #" & DateTimePicker1.Text & "# order by HostelerName", con)

    Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
    Dim myDataSet As DataSet = New DataSet()

    myDA.Fill(myDataSet, "Hostelers")

    DataGridView6.DataSource = myDataSet.Tables("Hostelers").DefaultView

    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
4

3 回答 3

2

尝试将“where”子句更改为:

" WHERE MONTH(DOB) = " & DateTimePicker1.Value.Month &
      " AND DAY(DOB) = " & DateTimePicker1.Value.Day 

但是我也强烈建议您使用参数化 SQL 命令 http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx

这将防止 SQL 注入(http://technet.microsoft.com/en-us/library/ms161953(v=SQL.105).aspx)和其他错误

于 2013-10-02T22:28:59.350 回答
2

“通过使用自定义格式 dd/mm 从日期时间选择器中选择 11/09 来搜索数据”

在 Access 查询中,您可以要求数据库引擎应用Format()到您的DOB值,然后进行比较。这将对静态值'11/09'执行此操作...

WHERE Format(DOB, 'dd/mm') = '11/09'

为了使其灵活,您可以使用参数查询和提供DateTimePicker1.Text作为参数值。

WHERE Format(DOB, 'dd/mm') = your_string
于 2013-10-02T22:36:47.627 回答
1

将您的查询更改为以下内容:

cmd = New OleDbCommand("
SELECT HostelerID as [Hosteler ID], HostelerName as [Hosteler Name], 
    DOB, HostelName as [Hostel Name],
    RoomNo as [Room No], DateOfJoining as [Date Of Joining], 
    Purpose, FatherName as [Father's Name],
    MobNo1 as [Mobile No], Phone1 as [Phone No], 
    MotherName as [Mother's Name], MobNo2 as [Mobile No 2],
    City, Address, Email, ContactNo as [Contact No], 
    InstOfcDetails as [Ins/Ofc Details], Phone2 as [Phone No 2],
    Agreement, GuardianName as  [Guardian Name],
    GuardianAddress as [Guardian Address], MobNo3 as [Guardian Mobile No],
    Phone3 as [Guardian Phone No], FixedDeposit as [Fixed Deposit],
    CompletionDate as [Completion Date], Photo, DocsPic as [Docs Pic] 
FROM Hostelers 
WHERE (DatePart(\"M\", DOB) = '" + DateTimePicker1.Value.Date.Month + '")
  AND (DatePart(\"D\", DOB) = '" + DateTimePicker1.Value.Date.Day + '"))
ORDER BY HostelerName
")
于 2013-10-02T22:29:15.367 回答