SQLite数据库错误删除查询

时间:2013-10-29 16:58:12

标签: c# database sqlite sql-delete

我正在使用C#和sqlite在visual studio上开发一个健身房会员计划。在每次员工登录尝试中,我希望程序检查过期客户,其中ExpiryDate(表Customer中的属性)小于今天的日期。这是我使用的代码片段:

string sql2 = "delete from Customer where ExpiryDate<' " + DateTime.Today + " ' ";
SQLiteCommand command2 = new SQLiteCommand(sql2, m_dbConnection);
command2.ExecuteNonQuery(); 

2 个答案:

答案 0 :(得分:3)

使用参数化查询,您将日期转换为字符串的问题将会消失

 string sql2 = "delete from Customer where ExpiryDate < @td"; 
 SQLiteCommand command2 = new SQLiteCommand(sql2, m_dbConnection); 
 command2.Parameters.AddWithValue("@td", DateTime.Today);
 command2.ExecuteNonQuery(); 

当然这也是避免Sql注入的推荐方法,但在这种情况下不是您主要关注的问题。

顺便说一句,我同意上述问题中的评论。可能最好在计算期限时允许一点灵活性。例如,您可以添加一个配置选项,设置截止日期之后允许的最大天数,并将其添加到DateTime.Today值。

答案 1 :(得分:3)

为什么不使用SQLite内置函数?

delete from Customer where ExpiryDate<DATE('NOW')

你是不是错过了像

这样的东西
delete from Customer where ExpiryDate<DATE('NOW') AND Costumer_id=?