Dealing with dd/mm/yyyy date formats using OleDb and Access

后端 未结 1 1111
滥情空心
滥情空心 2021-01-23 07:02

I\'m having problems dealing with dates in my queries/SQL code. My regional settings are in the format #dd/mm/yyyy#, so when inserting a date to access database it looks like #d

相关标签:
1条回答
  • 2021-01-23 07:28

    My regional settings are in the format #dd/mm/yyyy#, so when inserting a date to access database it looks like #dd/mm/yyyy#

    Edit: Thanks to the comment below from Plutonix I realize that your question is unclear as to whether you are referring to a VB.NET #date_literal# or an Access SQL #date_literal#. My answer is in regard to date literals in Access SQL:


    It is important to realize that the Access Database Engine ignores the Regional Settings in Windows and always interprets ambiguous #xx/yy/zzzz# date literals in Access SQL statements as #mm/dd/yyyy#. This can be illustrated with the following VB.NET code on a machine using the default "English (United Kingdom)" settings in Windows:

    Imports System.Data.OleDb
    Imports System.Globalization
    
    Module Module1
    
        Sub Main()
            Console.WriteLine("Culture name:")
            Console.WriteLine("    {0}", CultureInfo.CurrentCulture.Name)
            Console.WriteLine("Short date pattern:")
            Console.WriteLine("    {0}", CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern)
            Using con As New OleDbConnection
                con.ConnectionString =
                        "Provider=Microsoft.Jet.OLEDB.4.0;" &
                        "Data Source=C:\Users\Public\mdbTest.mdb;"
                con.Open()
                Using cmd As New OleDbCommand
                    cmd.Connection = con
                    ' try updating to November 2, 2010 as #dd/mm/yyyy#
                    cmd.CommandText =
                            "UPDATE Employees_Details " &
                            "SET EmpEndDate=#02/11/2010# " &
                            "WHERE EmpCode=1"
                    Console.WriteLine("UPDATE command:")
                    Console.WriteLine("    {0}", cmd.CommandText)
                    cmd.ExecuteNonQuery()
                    ' read value back and display
                    cmd.CommandText =
                            "SELECT EmpEndDate " &
                            "FROM Employees_Details " &
                            "WHERE EmpCode=1"
                    Dim EndDate As DateTime = Convert.ToDateTime(cmd.ExecuteScalar)
                    Console.WriteLine("Retrieved date:")
                    Console.WriteLine("    {0}", EndDate.ToLongDateString)
                End Using
                con.Close()
            End Using
        End Sub
    
    End Module
    

    That code produces

    Culture name:
        en-GB
    Short date pattern:
        dd/MM/yyyy
    UPDATE command:
        UPDATE Employees_Details SET EmpEndDate=#02/11/2010# WHERE EmpCode=1
    Retrieved date:
        11 February 2010
    

    showing that #02/11/2010# was written to the Access database as "11 February 2010", not "2 November 2010".

    A much better approach is to avoid date literals in Access SQL statements by using parameterized queries. If date literals must be used in Access SQL statements, always opt for the unambiguous #yyyy/mm/dd# format.

    0 讨论(0)
提交回复
热议问题