如何在vb.net WPF和MySQL中使用DataGrid来获取/过滤两个日期之间的数据?

时间:2017-06-21 06:43:58

标签: mysql wpf vb.net

如何在vb.net WPF和MySQL中使用Data Grid获取/过滤两个日期之间的数据?

Private Sub BtnSubmit_Click(sender As Object, e As RoutedEventArgs) Handles BtnSubmit.Click

    Mysqlconn = New MySqlConnection
    Mysqlconn.ConnectionString = "server=localhost; userid=root; password=ronaldo11; database= majlis_cms"


    Dim dtp1 As Date = datePicker1.SelectedDate.Value
    Dim dtp2 As Date = datePicker2.SelectedDate.Value

    dt.Clear()

    Try
        Mysqlconn.Open()
        Dim query As String

        query = "Select * from majlis_cms.daily_cash_balancing_report where Date between >= " & dtp1.ToString("yyyy") & "/" & dtp1.ToString("MM") & "/" & dtp1.ToString("dd") & "# and Date <= #" & dtp2.ToString("yyyy") & "/" & dtp2.ToString("MM") & "/" & dtp2.ToString("dd") & "# order by Date "

        da = New MySqlDataAdapter(query, Mysqlconn)
        da.Fill(dt)


        If dt.Rows.Count = 0 Then
            MessageBox.Show("No match found")
        End If

    Finally
        Mysqlconn.Dispose()
    End Try
    DataGrid.DataContext = dt
    Load_DataGrid()
End Sub

错误消息截图:

1 个答案:

答案 0 :(得分:0)

最后我明白了。这是代码。

导入MySql.Data.MySqlClient Imports System.Data

Public Class DailyCash     私有Mysqlconn作为新的MySqlConnection     私有命令为新的MySqlCommand     Private dt As New DataTable     Private da As MySqlDataAdapter     私有dtp1作为字符串     私有dtp2字符串     专用ds作为数据集

Public Sub New()

    InitializeComponent()

    Load_DataGrid()
End Sub

Private Sub Load_DataGrid()
    Dim connectionString As String = "server=localhost; database=majlis_cms; userid=root; password=ronaldo11;"

    Dim connection As New MySqlConnection(connectionString)
    Dim cmd As New MySqlCommand("Select * from majlis_cms.daily_cash_balancing_report", connection)
    connection.Open()

    dt.Load(cmd.ExecuteReader())
    connection.Close()

    dataGrid.DataContext = dt
End Sub

Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
    Dim MainWindow As New MainWindow

    MainWindow.Show()
    Hide()
End Sub

Private Sub BtnSubmit_Click(sender As Object, e As RoutedEventArgs) Handles BtnSubmit.Click

    Mysqlconn = New MySqlConnection
    Mysqlconn.ConnectionString = "server=localhost; userid=root; password=ronaldo11; database= majlis_cms;"

    Dim dtp1 As Date = datePicker1.SelectedDate.Value
    Dim dtp2 As Date = datePicker2.SelectedDate.Value

    dt.Clear()

    Dim query As String
    Mysqlconn.Open()
    query = "Select * from daily_cash_balancing_report where Date between'" & dtp1.ToString("yyyy") & "/" & dtp1.ToString("MM") & "/" & dtp1.ToString("dd") & "' and  '" & dtp2.ToString("yyyy") & "/" & dtp2.ToString("MM") & "/" & dtp2.ToString("dd") & "' order by Date "


    da = New MySqlDataAdapter(query, Mysqlconn)

    da.Fill(dt)
    ds = New DataSet

    dataGrid.DataContext = dt
    Mysqlconn.Close()


    If dt.Rows.Count = 0 Then
        MessageBox.Show("No match found")
    End If

    Mysqlconn.Dispose()


End Sub

enter screenshot 1 (before filter) description here

enter screenshot 2 (after filter) description here