Dapper:字符串未被识别为有效的 DateTime
Dapper: String was not recognized as a valid DateTime
我正在使用 Dapper 访问 sqlite 数据库并获取 'String was not recognized as a valid DateTime.'。感谢任何帮助。
// Create table schema
CREATE TABLE "mytable" ( "field1" TEXT, "field2" TEXT, "field3" TEXT,
"field4" TEXT, "field5" TEXT,
"field6" bit,
"field7" TEXT, "field8" TEXT, "field9" TEXT,
"field10" TEXT,
"field11" DateTime )
查询代码:
var result= sqliteConnection.Query<TestItem>("Select * from mytable");
插入数据的查询:
INSERT INTO "main"."mytable" ("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11") VALUES ('750eb223-2993-4d85-9d4f-3e8689e9baa7', 'some value', '', 'some value', 'some value', '1', '84', 'ae35e1e1-dd4c-4e49-a76c-d577f417bf9a', 'some value', 'HOME.aspx',
'2020/01/20 17:38');
INSERT INTO "main"."mytable" ("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11") VALUES ('750eb223-2993-4d85-9d4f-3e8689e9baa7', 'some value', 'asdf', 'some value', 'some value', '1', '32', 'a1cd1b8f-95f6-4b03-8d54-f904c21749ac', 'HOME.aspx', 'HOME.aspx',
'2020/01/20 17:38');
INSERT INTO "main"."mytable" ("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11") VALUES ('750eb223-2993-4d85-9d4f-3e8689e9baa7', 'some value', 'some value', 'some value', 'some value', '1', '99', 'b9e63bfd-c73e-4e9a-b3e7-30ae49d8a002', 'CALLSS.aspx', 'CALLSS.aspx',
'2020/01/20 17:38');
错误信息:
Error parsing column 10 (field11=HOME.aspx - String)
内部异常消息:
String was not recognized as a valid DateTime.
堆栈跟踪:
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in C:\projects\dapper\Dapper\SqlMapper.cs:line 3609
at Dapper.SqlMapper.<QueryImpl>d__138`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1100
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723
at Tzunami.LinkResolver.DatabaseMigration.Models.DBMigrator.<MigrateDeploymentListItemAsync>d__5.MoveNext() in C:\Users\surendra\source\repos\Tzunami.LinkResolver.MigrationTool\Tzunami.LinkResolver.DatabaseMigration\Models\DBMigrator.cs:line 77
映射项class:
public class TestItem
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
public string Field6 { get; set; }
public string Field7 { get; set; }
public string Field8 { get; set; }
public string Field9 { get; set; }
public string Field10 { get; set; }
public string Field11 { get; set; }
}
数据库架构和条目可以从以下从 DB SQLite 浏览器中获取的示例中看到
首先:请记住,如 Sqlite 文档中所述,没有 Datatypes
,因为 DateTime
它以某种方式存储为文本或一些数字,因此最好将日期保存为纯文本数据库创建。
此处的文档:https://www.sqlite.org/datatype3.html
其次:预计日期时间采用 ISO8601 格式,即“YYYY-MM-DD HH:MM:SS.SSS”格式,但作为 table 上的数据,它是“YYYY-MM-DD HH:MM" 它不能被解析为 DateTime
.
解决方法:
由于您无法更改数据库架构和数据,因此 connection.ExecuteReader
在这些情况下工作得很好,因此使用 connection.ExecuteReader
并使用 reader.GetString()
方法将列值作为字符串获取。
尝试使用此格式作为字符串。
'2007-01-01 10:00:00'
即yyyy-MM-dd HH:mm:ss
我从未使用过 SQLite,但根据 document,SQLite 没有可存储的数据类型 DateTime
。
2.2. Date and Time Datatype
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
- TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
- REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
- INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
考虑到以上,您在以下代码中的日期格式:
INSERT INTO "main"."mytable" (..., "field11") VALUES (..., '2020/01/20 17:38');
应该是
YYYY-MM-DD HH:MM:SS.SSS
这样,您不需要更改数据库架构;只需更改您的代码。
我正在使用 Dapper 访问 sqlite 数据库并获取 'String was not recognized as a valid DateTime.'。感谢任何帮助。
// Create table schema
CREATE TABLE "mytable" ( "field1" TEXT, "field2" TEXT, "field3" TEXT,
"field4" TEXT, "field5" TEXT,
"field6" bit,
"field7" TEXT, "field8" TEXT, "field9" TEXT,
"field10" TEXT,
"field11" DateTime )
查询代码:
var result= sqliteConnection.Query<TestItem>("Select * from mytable");
插入数据的查询:
INSERT INTO "main"."mytable" ("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11") VALUES ('750eb223-2993-4d85-9d4f-3e8689e9baa7', 'some value', '', 'some value', 'some value', '1', '84', 'ae35e1e1-dd4c-4e49-a76c-d577f417bf9a', 'some value', 'HOME.aspx',
'2020/01/20 17:38');
INSERT INTO "main"."mytable" ("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11") VALUES ('750eb223-2993-4d85-9d4f-3e8689e9baa7', 'some value', 'asdf', 'some value', 'some value', '1', '32', 'a1cd1b8f-95f6-4b03-8d54-f904c21749ac', 'HOME.aspx', 'HOME.aspx',
'2020/01/20 17:38');
INSERT INTO "main"."mytable" ("field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10", "field11") VALUES ('750eb223-2993-4d85-9d4f-3e8689e9baa7', 'some value', 'some value', 'some value', 'some value', '1', '99', 'b9e63bfd-c73e-4e9a-b3e7-30ae49d8a002', 'CALLSS.aspx', 'CALLSS.aspx',
'2020/01/20 17:38');
错误信息:
Error parsing column 10 (field11=HOME.aspx - String)
内部异常消息:
String was not recognized as a valid DateTime.
堆栈跟踪:
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in C:\projects\dapper\Dapper\SqlMapper.cs:line 3609
at Dapper.SqlMapper.<QueryImpl>d__138`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1100
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723
at Tzunami.LinkResolver.DatabaseMigration.Models.DBMigrator.<MigrateDeploymentListItemAsync>d__5.MoveNext() in C:\Users\surendra\source\repos\Tzunami.LinkResolver.MigrationTool\Tzunami.LinkResolver.DatabaseMigration\Models\DBMigrator.cs:line 77
映射项class:
public class TestItem
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
public string Field6 { get; set; }
public string Field7 { get; set; }
public string Field8 { get; set; }
public string Field9 { get; set; }
public string Field10 { get; set; }
public string Field11 { get; set; }
}
数据库架构和条目可以从以下从 DB SQLite 浏览器中获取的示例中看到
首先:请记住,如 Sqlite 文档中所述,没有 Datatypes
,因为 DateTime
它以某种方式存储为文本或一些数字,因此最好将日期保存为纯文本数据库创建。
此处的文档:https://www.sqlite.org/datatype3.html
其次:预计日期时间采用 ISO8601 格式,即“YYYY-MM-DD HH:MM:SS.SSS”格式,但作为 table 上的数据,它是“YYYY-MM-DD HH:MM" 它不能被解析为 DateTime
.
解决方法:
由于您无法更改数据库架构和数据,因此 connection.ExecuteReader
在这些情况下工作得很好,因此使用 connection.ExecuteReader
并使用 reader.GetString()
方法将列值作为字符串获取。
尝试使用此格式作为字符串。
'2007-01-01 10:00:00'
即yyyy-MM-dd HH:mm:ss
我从未使用过 SQLite,但根据 document,SQLite 没有可存储的数据类型 DateTime
。
2.2. Date and Time Datatype
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
- TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
- REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
- INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
考虑到以上,您在以下代码中的日期格式:
INSERT INTO "main"."mytable" (..., "field11") VALUES (..., '2020/01/20 17:38');
应该是
YYYY-MM-DD HH:MM:SS.SSS
这样,您不需要更改数据库架构;只需更改您的代码。