Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLite returns DATETIME column value as string
    primarykey
    data
    text
    <p>I used Visual Studio 2010 with the System.Data.SQLite data provider to create a schema where one of the tables 'mytable' has a column 'mydatetime' of type DATETIME. My application (using that provider) stored a C# nullable DateTime value (DateTime?) in the column. The table design clearly identifies the column type as 'datetime'. When the application performs an ExecuteScalar on the statement SELECT MAX(mydatetime) from mytable, the returned result is a string.</p> <p>I've read some other posts which discuss the SQLite implementation of date/time data, but I can't see how it applies here. Since the schema knows it is a datetime, shouldn't the data provider return the column value as a C# DateTime (or nullable DateTime, in this case), regardless of how the engine stores the value?</p> <p>Sample code per request: Not sure exactly what the create table SQL would look like because I did that via the VS2010 designer.</p> <p>As for the rest...</p> <pre><code>//Given factory/connection established/initialized as appropriate for my SQLite database... DbProviderFactory factory; DbConnection connection; // Add a row... DateTime? dt = DateTime.Parse("2013-06-17 12:25:00"); DbCommand cmd = connection.CreateCommand(); cmd.CommandText = "INSERT INTO MyTable(MyDateTime) VALUES (@datetime)"; DbParameter param = factory.CreateParameter(); param.ParameterName="@datetime"; param.Value = dt; cmd.Parameters.Add(param); cmd.ExecuteNonQuery(); // Get the max... DbCommand cmd2 = connection.CreateCommand(); cmd2.CommandText = "SELECT MAX(MyDateTime) FROM MyTable"; object result = cmd2.ExecuteScalar(); // Ideally, should be able to cast result to DateTime?, // but in reality, it's a string and has to be parsed back to DateTime first. // Other providers don't do this; Both OLEDB for Access and MySQL // return a boxed DateTime (or DateTime?). </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload