oracle如何从vb.net的基本日期选择器中读取日期?

时间:2013-06-10 06:22:37

标签: vb.net oracle

你好我正在使用oracle& vb.net

这是我的select语句代码,我想从界面中显示所选日期,我使用基本日期选择器,并将其指定为bdp1

 Dim queryString As String = "select * from abc where (tran_dttm <= to_date( '" & bdp1 & "' ,'MM/DD/YYYY') and tran_dttm > to_date( '" & bdp1 & "' ,'MM/DD/YYYY')and lpt = '" & ListBox1 & "' and device = '" & strMaterial & "')"

2 个答案:

答案 0 :(得分:0)

参数应以:为前缀,并应直接作为文本包含在命令中。框架的工作是将它们的值传递给数据库引擎

Private Function GetDate(ByVal strMaterial As String, ByVal ListBox1 As String, ByVal bdp1 As Date) As DataSet

    Dim connectionString As String = "Data Source = ***; User ID = ***; Password = **;"
    Dim queryString As String = "BEGIN select * from abc " & _
             "where (tran_dttm <= to_date(:bdp1,'MM/DD/YYYY') and " & _
             "tran_dttm > to_date(:bdp1 ,'MM/DD/YYYY') and " & _
             "lpt = :lb1 and device = :mat); END;"
    Using sqlConnection = New OracleClient.OracleConnection(connectionString)
    Using sqlCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
        sqlCommand.CommandTimeout = 0
        sqlCommand.Parameters.Add(New OracleParameter(":bdp1", OracleType.DateTime)).Value = bdp1
        sqlCommand.Parameters.Add(New OracleParameter(":lb1", OracleType.VarChar)).Value = lstBox1.SelectedItem.ToString 
        sqlCommand.Parameters.Add(New OracleParameter(":mat1", OracleType.VarChar)).Value = strMaterial
        Dim dataAdapter As OracleClient.OracleDataAdapter = New OracleClient.OracleDataAdapter(sqlCommand)
        Dim dataSet As DataSet = New DataSet
        dataAdapter.Fill(dataSet)
        Return dataSet
    End Using
    End Using
End Function

如果ListBox1是控件,则它是错误的。您应该使用SelectedItem属性作为参数的值(虽然需要进行一些错误检查)

答案 1 :(得分:0)

从abc中选择*其中trunc(tran_dttm)= to_date('“&amp; bdp1&amp;”','MM / DD / YYYY')