Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/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
C# sqlite expert personal中的日期时间错误_C#_Sqlite - Fatal编程技术网

C# sqlite expert personal中的日期时间错误

C# sqlite expert personal中的日期时间错误,c#,sqlite,C#,Sqlite,我正试图用c#从VisualStudio编写sqlite数据库。当使用datetime时。现在在VisualStudio中,它向我显示了正确的日期和时间,但在写入db时,它会显示类似于1899/30/12。为什么呢?这是我的密码: SQLiteCommand cmd = new SQLiteCommand("INSERT INTO tblLicenseInfo (ID, UserID, MachineID, ExpirationDate, DateOfChange, LicenseKey)

我正试图用c#从VisualStudio编写sqlite数据库。当使用datetime时。现在在VisualStudio中,它向我显示了正确的日期和时间,但在写入db时,它会显示类似于
1899/30/12
。为什么呢?这是我的密码:

SQLiteCommand cmd = new SQLiteCommand("INSERT INTO tblLicenseInfo (ID, UserID, MachineID, ExpirationDate, DateOfChange, LicenseKey)
                                       VALUES(1, 1, 1, @test, @test, 'sdfsafge45345345');", SqLite);

cmd.Parameters.Add("@test", DateTime.Now);
SqLite.Open();
cmd.ExecuteNonQuery();
SqLite.Close();
谢谢你的帮助。

我不太清楚为什么重载不起作用,因为它说:

将SQLiteParameter添加到给定 参数名称和值

作为替代方案,您可以使用其他重载或类似的方法

cmd.Parameters.Add("@test", SQLiteType.DateTime).Value = DateTime.Now;

更多信息

但是等一下。。您说过您的列类型是
DATETIME
,但在
SQLite
中没有类似的数据类型

SQLite没有为存储日期而预留的存储类 和/或时间。相反,SQLite的内置 能够将日期和时间存储为文本、实数或整数 价值观:

  • 文本为ISO8601字符串(“YYYY-MM-DD HH:MM:SS.SSS”)
  • 真实的朱利安日数字,根据序言,自公元前4714年11月24日格林威治中午以来的天数 公历
  • 整数表示Unix时间,自1970-01-01 00:00:00 UTC以来的秒数
请说出你专栏的真实类型。我假设
DateTime.Now
不适合这3种类型格式(或值),这就是为什么在SQLite中将
1899/30/12
用作“默认”日期的原因

日期和时间函数使用IS0-8601日期和时间的子集 格式。函数的作用是:返回以下格式的日期: YYYY-MM-DD

我觉得如果您将列类型设置为
TEXT
,并发送
DateTime。现在使用
YYYY-MM-dd
格式,您的代码应该可以工作

cmd.Parameters.Add("@test", SQLiteType.TEXT).Value = DateTime.Now.ToString("YYYY-MM-dd");

DateTime.Now的格式不正确,因此您有两个选项:

  • 使用
    当前\u时间戳
    作为值。这是一个SQLite关键字

  • 格式化Datetime:
    Datetime.Now.ToString(“yyyy-MM-dd-HH:MM:ss”)

您的
ExpirationDate
DateOfChange
列的类型是什么?所以cmd.Parameters.Add行有问题,对吗?
cmd.Parameters.Add("@test", SQLiteType.TEXT).Value = DateTime.Now.ToString("YYYY-MM-dd");