我正在将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年不是公认的日期时间。
任何帮助,将不胜感激。
慕标琳琳
相关分类