简体   繁体   中英

Datepart function in SQL query not working with OLEDB connection in VB.NET

I am trying to fetch Date, Month and Year separately from date column in Access Database.

I am using following code for it.

I don't know what is the problem with this, but either it shows me error or no data is returned.

I am new to OLEDB so I don't know if it is possible or not.

Please help.

And please show me alternatives if this way is incorrect.

    conn_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\MHV\Documents\Visual Studio 2012\Projects\UTS\UTS.mdb"

    conn = New OleDbConnection(conn_string)
    conn.Open()

    Grid_string = "SELECT datepart(mm,T_Date) from Transactions"

    Grid_cmd = New OleDbCommand(Grid_string, conn)
    RW_AD = New OleDbDataAdapter(Grid_cmd)
    Grid_DS = New DataSet

    Grid_cmd.Connection = conn
    Grid_cmd.CommandText = Grid_string

    RW_AD.Fill(Grid_DS, "Transactions")

    Grid_cmd.ExecuteNonQuery() 

    DataGridView1.DataSource = Grid_DS.Tables("Transactions").DefaultView

PS : Connection and other things are working fine. It only shows me error when I use datepart().

您可以尝试在报价中设置日期间隔吗?

Grid_string = "SELECT datepart(\"mm\",T_Date) from Transactions"

使用'代替"将解决问题:

Grid_string = "SELECT datepart('mm',T_Date) from Transactions"

尝试使用m代替mm

Grid_string = "SELECT datepart('m',T_Date) from Transactions"

Data Type returned is Int16. Cast to Int16 or you'll get error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM