I tried to read out a sample Northwind sqlite database but get an error on some tables with a datetime. Is this a problem of the database or of my System.Data.SQLite?
The Exception is somethin like: “the string is not a valid DateTime”
http://system.data.sqlite.org/
Of cource I can read out the data by myself with the correct converting of datetime but this is not as performant as reading it out via a simple dt.Load()
SQLiteCommand dbCommand = myConnector.CreateCommand();
dbCommand.CommandText = "SELECT * FROM " + tablename;
SQLiteDataReader executeReader = dbCommand.ExecuteReader(CommandBehavior.SingleResult);
DataTable dt = new DataTable();
dt.Load(executeReader); // <-- FormatException
” bei System.DateTimeParse.ParseExactMultiple(String s, String[]
formats, DateTimeFormatInfo dtfi, DateTimeStyles style)\r\n bei
System.DateTime.ParseExact(String s, String[] formats, IFormatProvider
provider, DateTimeStyles style)\r\n bei
System.Data.SQLite.SQLiteConvert.ToDateTime(String dateText,
SQLiteDateFormats format, DateTimeKind kind) in
c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteConvert.cs:Zeile
322.”
I need a good help to improve the current code.
Yep, I faced the same problem, and I’ve found a solution.
First of all you need to know why it happens.
http://www.sqlite.org/datatype3.html
**
**
For more date/time functions you can see this page:
http://www.sqlite.org/lang_datefunc.html
SOLUTION:
Is just use a datetime function to convert your field into a valid .NET datatable format. You can use anyone from the above link, in my case I choose:
TIP 1
If your query is on a view, and you can’t touch your db, create a different view or something like, you are in a little trouble because the problem is on the db side as I explain before. So the first solution is replace the column of a datatable before load (a little dirty work). Or you can use a Unix stamp on a text field, or any desired timestamp.
But I think the best solution is create your query on your application from your view:
TIP 2
You can try also with the SQLite Administrator and play with datetime functions before to check the format.
TIP 3
Be aware of your culture globalization also, you can find a problem if you change the culture. Try to your soft work with only one and display it as you want, not as the culture wants. In my country for eg, we use as common this format: dd/MM/yy and is a pain, like the dot on decimal numbers (we use a comma)
TIP 4
If you are working with a wizard-defined connection on some report or entity or something like, be sure to check your desired conversion format. For example I use the most common and non problematic way: ISO8601