How to query for date and time based on date in C #

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

.

I want the query to be date based like:

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

      

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

.

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

+1


a source to share


2 answers


Here is some sample code using C # in a console application 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);
}

      

+1


a source


The question is not in the programming language, but in the request for access to mdb. Access requires a word DateValue

before entering a date:



string myQuery = "Select * FROM tableName WHERE date= DateValue ('03/02/2009')"; 

      

0


a source







All Articles