Storing and retrieving dates from MS Access using C#

csharp , db Egypt
  • 6 years ago

     Hello,

    I am facing difficulty in storing date in MS Access using C# code. I have tried to save the DateTime object into the database. The column in the database was also of DateTime datatype. but I could not save the date. It gave me syntax error.

    Then I tried to save the date as a String. But this time the date got saved in the database as some decimal. Plz help me in this regard. How to save the date in MS Access.

    Thanking you,

    Rakesh.

  • 6 years ago

     Hi,

    Here's my code:

    I am using C#  in Visual Studio 2005.

                DateTime currentDate = DateTime.Now;
                String input = currentDate.ToShortDateString();
               
               
                string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\student.mdb";
                OleDbConnection myConnection = new OleDbConnection(connectionString);
                myConnection.Open();
               
                string query = "insert into student VALUES ("+ input +")";

                OleDbCommand myCommand = new OleDbCommand();
                myCommand.CommandText = query;
                myCommand.Connection = myConnection;
                myCommand.ExecuteNonQuery();

                myConnection.Close();

    Thanks,

    Rakesh.

  • 6 years ago

     Thank you very much Bart. Its working fine now. I have one more problem. Is it possible to display multi-line list items in a list box in c# ? I need it in my application. Could you please help me out?

    Thanks,

    Rakesh.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan