tags:

views:

684

answers:

1

I have an MS-Access database with a DateTime column.
ex: 03/08/2009 12:00:00 AM.

I want query based on date like:

select * from tablename where date='03/08/2009'

I want display data as 03/08/2009 12:00:00 AM.

How would I write this query in C#? Please help me.

+2  A: 

Here's some sample code using C# in a console app to access an Access DB. You can adapt this code to windows or ASP.NET if needed.

/* Replace with the path to your Access database */
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;";

try
{
using(OleDbConnection conn = new OleDbConnection(connectionString)
{
   conn.Open();       
   string myQuery = "Select * FROM tableName WHERE date='03/02/2009'";       
   OleDbCommand cmd = new OleDbCommand(myQuery, conn);
   using(OleDbDataReader reader = cmd.ExecuteReader())
   {
      //iterate through the reader here
      while(reader.Read())
      {
         //or reader[columnName] for each column name
         Console.WriteLine("Fied1 =" + reader[0]); 
      }
   }
}

}
catch (Exception e)
{
   Console.WriteLine(e.Message);
}
ichiban