使用VB.net在Access数据库中按特定日期搜索

问题描述 投票:0回答:2

我尝试在 Access 数据库中使用 DateTimePicker 搜索日期并将结果显示在 DataGridView 中,但它不起作用。没有错误,但它只是不显示数据库/结果。以下是数据库和不起作用时的调试表单的图片,以防万一它们有任何用处:databaseform

这是我的代码:

Imports System.Data.OleDb
Public Class Form10
Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Daily Sales.accdb;")

Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    If connection.State = ConnectionState.Closed Then
        connection.Open()
    End If
    Dim dt1 As DateTime = DateTime.Parse(DateTimePicker1.Value)
    Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Table1] where [TDateField] = #" & dt1.ToString("MM/dd/yyyy"), connection)
    Dim da As New OleDbDataAdapter
    da.SelectCommand = cmd
    DataGridView2.DataSource = da
    connection.Close()
End Sub

Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DateTimePicker1.Format = DateTimePickerFormat.Custom
    DateTimePicker1.CustomFormat = "MM/dd/yyyy"
End Sub

End Class
database vb.net ms-access datagridview datetimepicker
2个回答
0
投票

使用日期时间选择器的真实日期值:

Dim dt1 As DateTime = DateTimePicker1.Value
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Table1] where [TDateField] = #" & dt1.ToString("yyyy'/'MM'/'dd") & "#", connection)

0
投票

MS ACCESS 通过 VB.net 访问日期代码是 “从表中选择 *,其中 tdate=#” & 格式(dtpickr.value,“MM/dd/yyyy”)&“#

© www.soinside.com 2019 - 2024. All rights reserved.