我正在尝试使用以下方法查询我的数据库:
connection.Query(@"SELECT timestamp FROM hdd_local_data_v1_2 WHERE date = " +
date.ToString("yyyy-MM-dd") +
" AND measured_dist = bit_loc AND rop > 0")
我的问题是它产生的字符串看起来像:
SELECT timestamp FROM hdd_local_data_v1_2 WHERE date = 2012-08-21 AND measured_dist = bit_loc AND rop > 0
在SQLite中运行此查询,我得到0结果。但是,如果我跑:
// Quotes around the date
SELECT timestamp FROM hdd_local_data_v1_2 WHERE date = "2012-08-21" AND measured_dist = bit_loc AND rop > 0
我得到了我期待的结果。我意识到我可以通过使用获得我期望的结果:
connection.Query("SELECT timestamp FROM hdd_local_data_v1_2 WHERE date = \"" +
date.ToString("yyyy-MM-dd") +
"\" AND measured_dist = bit_loc AND rop > 0")
我的问题是,是否可以在不使用转义\
的情况下获得预期的查询?在更大程度上,有没有办法在建立连接后使用LINQ查询数据库?