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

╄→尐↘猪︶ㄣ 提交于 2019-12-18 09:11:32

问题


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.

SELECT 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.


回答1:


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'



回答2:


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



来源:https://stackoverflow.com/questions/4720788/month-name-of-date-field-in-access-database-from-external-application-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!