【发布时间】:2021-10-14 14:27:29
【问题描述】:
我在消息表中有一个 Sqlite 行 createDate。我想从方法中createDate 介于startDate 和endDate 局部变量之间的消息中检索所有记录。
任何建议,如何为string stm 编写查询?或任何其他方式如何做到这一点
DateTime endDate = DateTime.Now;
DateTime startDate = endDate.AddDays(-7);
using (SQLiteConnection con = new SQLiteConnection(cs))
{
con.Open();
string stm = "SELECT * FROM messages WHERE createDate BETWEEN @startDate AND @endDate";
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
for (j = 0; j <= rdr.FieldCount - 1; j++)
{
data = rdr.GetValue(j).ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
i++;
}
}
}
con.Close();
}
【问题讨论】:
-
声明中没有绑定
@startDate和@endDate? -
@AKX 是的,我想将数据库中的 createDate 行与局部变量 startDate 和 endDate 进行比较,但不知道如何正确编写查询