theodore26的博客通过本文主要向大家介绍了sqlite3,DateTime Field等相关知识,希望本文的分享对您有所帮助
遇到的问题:
使用SQLite3.0时,插入的时间(DateTime)字段的值总是显示为1899-12-30,使用可视化工具编写sql语句为时间字段赋值为datetime('now')也还是显示1899-12-30。
建表语句:
Create table WordTable (id integer PRIMARY KEY autoincrement , word TEXT , updatedate datetime )
原来失败的代码片段:
SQLiteConnection dbConnection = new SQLiteConnection(@"Data Source=D:\\dbFile.db;Version=3;"); dbConnection.Open(); string strInsertSql = "insert into WordTable (id,word,updatedate) values (null,@word,@updatedate)"; SQLiteCommand command = new SQLiteCommand(strInsertSql, dbConnection); command.Parameters.Add(new SQLiteParameter("@word", DbType.String)); command.Parameters.Add(new SQLiteParameter("@updatedate", DbType.DateTime)); command.Parameters[0].Value = "测试"; command.Parameters[1].Value = DateTime.Now; command.ExecuteNonQuery();
在网上查找解决方案,有人说时间需要使用“2017-08-27T17:00:00”这样的格式,或者DateTime.Now.toString("s"),但是这两者尝试后都没有成功。又看到有人说列需要使用SmallDateTime类型。使用可视化工具修改表格列的类型,修改时间成功。不过似乎这个字段支持的时间范围是1900-2079年,精确到分。在修改代码时发现DbType中还有一个DateTime2枚举类型,将原代码中DateTime改为DateTime2后再测试,插入和修改时间成功。
MSDN中描述DateTime2为:日期和时间数据。日期值范围从公元 1 年 1 月 1 日到公元 9999 年 12 月 31 日。时间值范围从 00:00:00 到 23:59:59.9999999,精度为100 毫微秒