C# 基于日期从sqlite检索数据
我正在使用c#中的以下查询从Sqlite数据库检索数据 它很好用。但我想根据日期检索数据,如:C# 基于日期从sqlite检索数据,c#,sqlite,datetime,C#,Sqlite,Datetime,我正在使用c#中的以下查询从Sqlite数据库检索数据 它很好用。但我想根据日期检索数据,如: SELECT * FROM tableName WHERE date=09-09-2013 但它不适合我,因为Sqlite日期表示不是这种日期格式。 我想问的是,是否有任何方法可以根据上述查询中提到的用户日期和时间检索Sqlite数据,以及如何以用户可读的格式表示Sqlite数据库的日期和时间 参数化查询将使您的代码免于各种数据库引擎要求的日期、字符串和小数格式 using (SqliteConne
SELECT * FROM tableName WHERE date=09-09-2013
但它不适合我,因为Sqlite日期表示不是这种日期格式。
我想问的是,是否有任何方法可以根据上述查询中提到的用户日期和时间检索Sqlite数据,以及如何以用户可读的格式表示Sqlite数据库的日期和时间 参数化查询将使您的代码免于各种数据库引擎要求的日期、字符串和小数格式
using (SqliteConnection con = new SqliteConnection(connectionString))
{
con.Open();
string commandText = "SELECT * FROM tableName WHERE date=@dt";
using (SqliteCommand cmd = new SqliteCommand(commandText, con))
{
cmd.Parameters.AddWithValue("@dt", yourDateVariable)
SqliteReader reader = cmd.ExecuteReader();
while(reader.Read())
{
// Extract your data from the reader here
.....
}
}
}
本例的重点是展示如何构建参数化查询。通过这种方式,您可以将datetime变量的值传递给Sqlite引擎的框架,该框架更了解如何为底层系统格式化日期
在
commandText
变量中,占位符@dt
获取日期的实际格式值,然后将一个与占位符同名的参数和日期变量中的值添加到SqliteCommand参数集合中 您可能希望在sqlite中搜索更多此strftime
。使用参数化查询。09-09-2013
是三个相互减去的数字。也许您想使用字符串'09-09-2013'
?
using (SqliteConnection con = new SqliteConnection(connectionString))
{
con.Open();
string commandText = "SELECT * FROM tableName WHERE date=@dt";
using (SqliteCommand cmd = new SqliteCommand(commandText, con))
{
cmd.Parameters.AddWithValue("@dt", yourDateVariable)
SqliteReader reader = cmd.ExecuteReader();
while(reader.Read())
{
// Extract your data from the reader here
.....
}
}
}