【问题标题】:How to save date, datetime and time to SQLite Database in Xamarin Forms?如何在 Xamarin Forms 中将日期、日期时间和时间保存到 SQLite 数据库?
【发布时间】:2019-02-26 22:38:15
【问题描述】:

我想在我的 SQLite 数据库中插入一个日期、日期时间(没有秒数)和时间。我可以插入数据,但信息错误。保存数据的变量是正确的,但是当数据插入数据库时​​,信息是错误的。

我有这张桌子:

[表格(“tblCaf”)]
公共类 CAFTable
{
[PrimaryKey, MaxLength(100)]
公共字符串 CAFNo { 获取;放; }
公共 int EmployeeID { 获取;放; }
公共日期时间 CAFDate { 获取;放; }
[最大长度(100)]
公共字符串 CustomerID { 获取;放; }
公共日期时间开始时间 { 得到;放; }
公共日期时间结束时间 { 获取;放; }
公共字符串 Photo1 { 获取;放; }
公共字符串 Photo2 { 获取;放; }
公共字符串 Photo3 { 获取;放; }
公共字符串 MobilePhoto1 { 获取;放; }
公共字符串 MobilePhoto2 { 获取;放; }
公共字符串 MobilePhoto3 { 获取;放; }
[最大长度(1000)]
公共字符串 备注 { get;放; }
[最大长度(1000)]
公共字符串 OtherConcern { 获取;放; }
公共日期时间 LastSync { 获取;放; }
公共日期时间服务器更新 { 获取;放; }
公共日期时间 MobileUpdate { 获取;放; }
}

这是我的代码:

var caf = entCafNo.Text;
var retailerCode = entRetailerCode.Text;
var employeeNumber = entEmployeeNumber.Text;
var date = dpDate.Date; //I get the date inside the datepicker
var startTime = tpTime.Time; //I get the time inside the time picker
var endTime = DateTime.Now.TimeOfDay; //I get the current time
var photo1url = entPhoto1Url.Text;
var photo2url = entPhoto2Url.Text;
var photo3url = entPhoto3Url.Text;
var otherconcern = entOthers.Text;
var remarks = entRemarks.Text;
var current_datetime = DateTime.Now.ToString("yyyy-MM-dd hh:mm"); //I get the current datetime

string caf_sql = "INSERT INTO tblCaf(CAFNo, EmployeeID, CafDate, CustomerID, StartTime, EndTime, Photo1, Photo2, Photo3, Remarks, OtherConcern, LastSync, MobileUpdate) 
VALUES('" + caf + "','" + employeeNumber + "', '" + date + "', '" + retailerCode + "', '" + startTime + "', '" + endTime + "', '" + photo1url + "', '" + photo2url + "', '" + photo3url + "', '" + remarks + "', '" + otherconcern + "', '" + current_datetime + "', '" + current_datetime + "')";

await conn.ExecuteAsync(caf_sql);

我可以得到正确的日期、时间和日期时间。问题是当我保存此日期时,日期变为 01/01/0001,时间变为 00:00:00,日期时间变为 01/01/0001 00:00:00,换句话说,数据未正确添加。我可以对我的代码进行哪些改进?

【问题讨论】:

  • 您是否有理由使用插入查询而不是使用插入方法?
  • 插入方法怎么做?
  • 等待 conn.InsertAsync(obj);其中 obj 是 CAFTable - 请参阅文档:github.com/praeclarum/sqlite-net
  • 您还使用 TimeSpan 对象作为时间并将它们插入到 DateTime 字段中,这将不起作用
  • @Jason 我不明白你能给我看一个代码来改进我的代码吗?

标签: datetime xamarin xamarin.forms sqlite


【解决方案1】:

不用手动构建插入语句,使用插入函数可能会获得更好的结果

var item = new CAFTable {
  CAFDate = dpDate.Date,
  StartTime = tpTime.Date, // note Time is a TimeSpan, not a DateTime
  EndTime = DateTime.Now, // note TimeOFDay is a TimeSpan, not a DateTime
  LastSync = DateTime.Now,
  MobileUpdate = DateTime.Now
  ... set any other properties as needed
};

await conn.InsertAsync(item);

【讨论】:

  • 所以我需要将 StartTime 和 EndTime 的数据类型更改为 Timespan 以便 tpTime.Time 工作?
  • 日期时间怎么样? LastSync 的格式例如 2018-08-21 12:30:00var current_datetime = DateTime.Now.ToString("yyyy-MM-dd hh:mm"); b> 好吗?
  • 它已经在您的数据库中定义为 DateTime,为什么要先将其转换为字符串?只需将其存储为 DateTime。
  • 只是 DateTime.Now;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 2016-08-17
  • 2019-04-02
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
相关资源
最近更新 更多