如何使用日期时间选择器值使用VB.NET从数据库访问数据

时间:2014-04-07 06:51:21

标签: vb.net ms-access datetimepicker

我创建了一个包含两个日期时间选择器的表单,并希望在从datetime选择器中选择的两个特定日期之间访问存储在Access数据库中的数据。

有人可以点击按钮

来帮助解决这个问题
    objDoc = gobjGeoApp.document
    Dim GeoApp As GeoMedia.Application
    GeoApp = gobjGeoApp
    objDoc.Close()

    GeoApp.Open("C:\GeoWorkspaces\PowerPOCfinal.gws")

    objDoc.Connections.Add("Connect1")

    With objDoc.Connections.item(1)
        .Location = "C:\Warehouses\prjct_mntrng.mdb"

        .Description = "PM Data"
        .Mode = PClient.ConnectionConstants.gmcModeReadWrite
        .Type = "Access.GDatabase"
        .Connect()
    End With

    With ListBox1
        .Items.Add("Full Name = " & objDoc.Connections(1).Name)
        .Items.Add("Name = " & objDoc.Name)
        .Items.Add("Path = " & objDoc.Path)
        .Items.Add("Storage = " & objDoc.Storage)
    End With

End Sub


Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    ' Create and display recordset of Actual Start
    objConnect = objDoc.Connections.item(1)
    objConnect.CreateOriginatingPipe(objOPipe1)

    With objOPipe1
        .GeometryFieldName = "Geometry"
        .Table = "Electric_Cables"
        .Filter = "((Electric_Cables.Actual_Start)<=" + DateTimePicker1.Value.Date() + " And (Electric_Cables.Actual_Start)<=" + DateTimePicker2.Value.Date() + ")"
        objRS = objOPipe1.OutputRecordset
        objRS.MoveLast()
        objRS.MoveFirst()
        MsgBox(objOPipe1.OutputRecordset.RecordCount)

    End With
End Sub

2 个答案:

答案 0 :(得分:0)

On按钮单击执行此操作,我只需根据您的要求在Gridview控件中绑定数据使用DataSet中的数据

  Dim connection As SqlConnection = New sqlconnection()
  connection.ConnectionString = "YOUR CONNECTION STRING"
  connection.Open()
  Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from Table where Date between DateTimePicker1.Value and DateTimePicker2.Value", connection)
  Dim ds As DataSet = New DataSet()
  adp.Fill(ds)
  DataGridView1.DataSource = ds.Tables(0)

答案 1 :(得分:0)

您需要将控件中的值与SQL语句的其余部分连接起来,如下所示:

"select * from Table where Date between" + DateTimePicker1.SelectedDate.ToString() + " and " + DateTimePicker2.SelectedDate.ToString()

您还可以考虑添加一个ASP SqlDataSource,它将是这样的:

       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [ID], [mydt], [Field1] FROM [Table1] WHERE (([mydt] &gt;= ?) AND ([mydt] &lt;= ?))">
            <SelectParameters>
                <asp:ControlParameter ControlID="Calendar1" Name="mydt" PropertyName="SelectedDate" Type="DateTime" />
                <asp:ControlParameter ControlID="Calendar2" Name="mydt2" PropertyName="SelectedDate" Type="DateTime" />
            </SelectParameters>
        </asp:SqlDataSource>