为了账号安全,请及时绑定邮箱和手机立即绑定

C#sqlite读取器不会在日期时间返回第一个连字符后的任何内容

C#sqlite读取器不会在日期时间返回第一个连字符后的任何内容

C#
摇曳的蔷薇 2021-03-30 09:22:50
我正在将DateTime属性插入sqlite数据库中,并在适当的时间成功将其存储为2018-04-09T00:00:00.0000000-03:00。当我尝试从数据库中检索它时,它仅返回2018。为什么它不返回完整值?我的插入是:using (SQLiteConnection conn = new SQLiteConnection(Utility.CONN_STR))        {            conn.Open();            SQLiteCommand cmd = new SQLiteCommand("INSERT into Waybill (createdOn) VALUES(@createdOn)", conn);            cmd.Parameters.AddWithValue("@createdOn", waybillP.CreatedOn);            cmd.ExecuteNonQuery();         }createdOn是一个DateTime。我的选择是:using (SQLiteConnection conn = new SQLiteConnection(Utility.CONN_STR))        {            conn.Open();            SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Waybill where WaybillId = (select max(waybillId) from Waybill);", conn);            SQLiteDataReader reader = cmd.ExecuteReader();            while (reader.Read())            {                int waybillId = Convert.ToInt16(reader["waybillId"].ToString());                var temp = reader["createdOn"].ToString();                DateTime createdOn = DateTime.Parse(temp);                waybill.WaybillId = waybillId;                waybill.CreatedOn = createdOn;            }Temp仅返回2018,而不是2018-04-09T00:00:00.0000000-03:00。我尝试将DB列设置为text,real和integer,结果相同。我也尝试过使用SQLite日期时间格式,例如:SQLiteCommand cmd = new SQLiteCommand("INSERT into Waybill (createdOn) VALUES(datetime('now'))", conn);无论数据库中的实际条目是什么样,每次尝试都将返回2018。这会导致DateTime.Parse崩溃,因为2018年不是公认的日期时间。任何帮助,将不胜感激。
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

Chetan的解决方案为我工作,因为我只返回一个结果,并且我知道读者的内容能够选择索引。

var createdOn = reader.GetDateTime(1);


查看完整回答
反对 回复 2021-04-10
  • 2 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信