sqlite的扔"字符串未被识别为有效的datetime"字符串、未被、有效、sqlite

2023-09-04 00:12:51 作者:七曜の贤者

我玩弄使用SQLite,并不断尝试读回一些测试数据时,得到一个错误。例如,我创建了一个简单的数据库有一个表和一些列填充了一些测试数据如下图所示。

I am playing around with Sqlite and keep getting an error when trying to read back some test data. For example, I created a simple db with a single table and some columns and populated it with some test data as shown below.

sqlite> .schema
CREATE TABLE "shows"(id integer primary key asc autoincrement, showName TEXT, guest TEXT, dateAired date, dateWatched date);

sqlite> select * from shows;
6|test show|test guest 1|2012.05.01|2012.07.10
7|test show|test guest 2|2012.05.02|2012.07.10
8|test show|test guest 4|2012.05.04|2012.07.10

我现在用的是System.Data.Sqlite库中提供的此处,但它不断给我一个错误,而试图读取日期列。我试图把日期的DD-MM-yyyy格式,但仍得到一个错误说字符串未被识别为有效日期时间。我已经使用DateTime.Parse或铸造日期​​时间或者只是的ToString()'荷兰国际集团它来看看会发生什么试过,但我不断收到同样的错误。我可以阅读文本字段罚款,但不能读取日期字段。

I am using the System.Data.Sqlite library available here , but it keeps giving me an error while trying to read the date column. I tried putting the dates in the dd-MM-yyyy format, but still get an error saying "String Not Recognized as a valid datetime." I have tried using DateTime.Parse or casting it to datetime or just ToString()'ing it to see what happens, but I keep getting the same error. I can read the text fields fine, but can't read the date fields.

我的C#code文档片断下面给出

My C# code snipped is given below

var sqliteConn = new SQLiteConnection("Data Source=data/shows.db;Version=3;New=False;Compress=True");
sqliteConn.Open();
SQLiteCommand cmd = new SQLiteCommand(sqliteConn);
cmd.CommandText = "select * from shows";

SQLiteDataReader reader = cmd.ExecuteReader( );
while (reader.Read( ))
    {
    var showName = reader["showName"];
    // This is where it keeps giving me an error.
    // I have tried various things such as DateTime.Parse
    var airedDate = DateTime.ParseExact("yyyy.MM.dd", reader["dateAired"].ToString(), new CultureInfo("en-AU"));
     }
reader.Close( );

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

问候,

推荐答案

根据在这个主题的讨论,我决定建立我的连接字符串以datetimeformat参数和字符串未被识别为有效日期时间的问题就解决了​​。

Based on the discussion in this thread, I decided to construct my connection string with the "datetimeformat" parameter and the "String Not Recognized as a valid datetime" issue was resolved.

我的例子连接字符串:

source=<source to db file>;version=3;new=False;datetimeformat=CurrentCulture

此功能是在2013年7月8日发布了1.0.87.0版本(见的发行说明)

This feature was released in version 1.0.87.0 on July 8, 2013 (see release notes)