How do I insert and query a DateTime object in SQLite DB from C# ?

Posted by Soham on Stack Overflow See other posts from Stack Overflow or by Soham
Published on 2010-03-17T02:00:51Z Indexed on 2010/03/17 2:01 UTC
Read the original article Hit count: 833

Filed under:
|
|

Hi All,

Consider this snippet of code:

string sDate = string.Format("{0:u}", this.Date);

           Conn.Open();
            Command.CommandText = "INSERT INTO TRADES VALUES(" + "\"" + this.Date + "\"" + "," +this.ATR + "," + "\"" + this.BIAS + "\"" + ")";
            Command.ExecuteNonQuery();

Note the "this.Date" part of the command. Now Date is an abject of type DateTime of C# environment, the DB doesnt store it(somewhere in SQLite forum, it was written that ADO.NET wrapper automatically converts DateTime type to ISO1806 format)

But instead of this.Date when I use sDate (shown in the first line) then it stores properly.

My probem actually doesnt end here. Even if I use "sDate", I have to retrieve it through a query. And that is creating the problem

Any query of this format

SELECT * FROM <Table_Name> WHERE DATES = "YYYY-MM-DD"

returns nothing, whereas replacing '=' with '>' or '<' returns right results.

So my point is:

How do I query for Date variables from SQLite Database.

And if there is a problem with the way I stored it (i.e non 1806 compliant), then how do I make it compliant

© Stack Overflow or respective owner

Related posts about sqlite

Related posts about c#