Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime 如何将日期、日期时间和时间保存到Xamarin表单中的SQLite数据库?_Datetime_Xamarin_Xamarin.forms_Sqlite - Fatal编程技术网

Datetime 如何将日期、日期时间和时间保存到Xamarin表单中的SQLite数据库?

Datetime 如何将日期、日期时间和时间保存到Xamarin表单中的SQLite数据库?,datetime,xamarin,xamarin.forms,sqlite,Datetime,Xamarin,Xamarin.forms,Sqlite,我想在SQLite数据库中插入日期、日期时间(不含秒)和时间。我可以插入数据,但信息是错误的。保存数据的变量是正确的,但将数据插入数据库时,信息是错误的 我有这张桌子: [表(“tblCaf”)] 公共类CAFTable { [主密钥,最大长度(100)] 公共字符串CAFNo{get;set;} public int EmployeeID{get;set;} 公共日期时间CAFDate{get;set;} [MaxLength(100)] 公共字符串CustomerID{get;set;} 公

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

我有这张桌子:

[表(“tblCaf”)]
公共类CAFTable
{
[主密钥,最大长度(100)]
公共字符串CAFNo{get;set;}
public int EmployeeID{get;set;}
公共日期时间CAFDate{get;set;}
[MaxLength(100)]
公共字符串CustomerID{get;set;}
公共日期时间开始时间{get;set;}
公共日期时间结束时间{get;set;}
公共字符串Photo1{get;set;}
公共字符串Photo2{get;set;}
公共字符串Photo3{get;set;}
公共字符串MobilePhoto1{get;set;}
公共字符串MobilePhoto2{get;set;}
公共字符串MobilePhoto3{get;set;}
[MaxLength(1000)]
公共字符串备注{get;set;}
[MaxLength(1000)]
公共字符串OtherConcern{get;set;}
public DateTime LastSync{get;set;}
公共日期时间服务器更新{get;set;}
公共日期时间移动更新{get;set;}
}

这是我的代码:

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,换句话说,数据添加不正确。我可以对代码进行哪些改进?

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

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);

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

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);

是否有什么原因导致您不使用insert方法而执行insert查询?如何执行insert方法?等待conn.InsertAsync(obj);其中obj是CAFTable——请参阅文档:您还使用TimeSpan对象来表示时间,并将它们插入DateTime字段中,而DateTime字段不会work@Jason我不明白,你能给我看一个代码来改进我的代码吗?你做插入查询而不是使用插入方法有什么原因吗?我怎么做插入方法?等待conn.InsertAsync(obj);其中obj是CAFTable-请参阅文档:您还使用TimeSpan对象表示时间,并将其插入DateTime字段中,而DateTime字段不会work@Jason我不明白,你能给我一个代码来改进我的代码吗?所以我需要将StartTime和EndTime的数据类型更改为Timespan,以便tpTime。工作时间?datetime如何?Las例如,tSync的格式是2018-08-21 12:30:00是var current\u datetime=datetime.Now.ToString(“yyy-MM-dd-hh:MM”);好吗?它已经在数据库中定义为DateTime,为什么要先将其转换为字符串?只需将其存储为DateTime.Just DateTime.Now;因此我需要将StartTime和EndTime的数据类型更改为Timespan,以便使用tpTime。工作时间?DateTime如何?例如,LastSync的格式为2018-08-21 12:30:00是var current_DateTime=DateTime.Now.ToString(“yyyy-MM-dd hh:MM”);好吗?它已经在数据库中定义为日期时间,为什么要先将其转换为字符串?只需将其存储为DateTime.Just-DateTime.Now;