开发者

Month name of date field in Access database from external application (C#)

I want to populate a drop down list at runtime in C#. I have a Date/Time field (e.g., 01/05/2000) in an Access 2003 database. I want to have the month name (i.e. January, February, ...) in the drop down list at runtime.

开发者_StackOverflowSELECT DISTINCT MonthName(Month(DATE_OF_BOOKING)) AS MNTH 
    FROM TRAVEL_DETAILS WHERE YEAR(DATE_OF_BOOKING)='2008' 

The above query works fine while I'm running it from Access directly, but when I'm trying to run it from an OledbCommand object in C# it says

Undefined function 'MonthName' in expression.


Text from social msdn thread:

Custom user-written VBA functions as well as many built-in VBA language functions are executed by Microsoft Access when embedded in a SQL query. Unfortunately the functions available to the Jet database engine are limited (when executed via ADO, ADO.NET, etc.). You can find a list of those available in the following MS KB article:

How to configure Jet 4.0 to prevent unsafe functions from running in Access 2003.

EDIT: Use Format() method.

SELECT DISTINCT  format(DATE_OF_BOOKING,'MMMM') 
   AS MNTH FROM TRAVEL_DETAILS WHERE YEAR(DATE_OF_BOOKING)='2008'


MonthName is one of the many Access functions that was not supported by the ODBC and OLEDB drivers for the original "Jet" database engine, i.e.

Driver={Microsoft Access Driver (*.mdb)} for ODBC, or
Provider=Microsoft.Jet.OLEDB.4.0 for OLEDB

but it is supported by the drivers for the newer "ACE" database engine

Driver={Microsoft Access Driver (*.mdb, *.accdb)} for ODBC, or
Provider=Microsoft.ACE.OLEDB.12.0 for OLEDB

Microsoft Access Database Engine 2016 Redistributable
Microsoft Access Database Engine 2010 Redistributable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜