【发布时间】: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