c# - 比较 SQL C# 中的日期

标签 c# mysql sql date

当我想获取特定日期的数据时,我的代码出现问题! 我的sql语句:

MySqlCommand cmd = new MySqlCommand("select a.acc_id,a.acc_Name,a.acc_Place,b.type,
b.acc_sub_debt,b.acc_sub_payment,
b.acc_sub_returned,b.acc_sub_date,COALESCE(b.acc_sub_note,' ') from accounts a, 
account_Details b where a.acc_ID = b.acc_Sub_ID 
where b.acc_sub_date = '" + DateTime.Parse(dateTimePicker1.Text).ToShortDateString()
 + "' order by b.id desc", objConn);

错误是:

"You have an error in your SQL syntac check the manual that corresponds to your MySQL server version for the right syntax to use near 'where b.acc_sub_date = '14/2/2015' order by b.id desc' at line 1"

最佳答案

这样做:

string sql = 
 @"select a.acc_id,a.acc_Name,a.acc_Place,b.type,b.acc_sub_debt,b.acc_sub_payment,b.acc_sub_returned,b.acc_sub_date,COALESCE(b.acc_sub_note,' ') 
   from accounts a, account_Details b 
   where a.acc_ID = b.acc_Sub_ID and b.acc_sub_date = @subDate 
   order by b.id desc";

MySqlCommand cmd = new MySqlCommand(sql, objConn);
cmd.Parameters.Add("@subDate", MySqlDbType.Date).Value = DateTime.Parse(dateTimePicker1.Text);

关于c# - 比较 SQL C# 中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28518489/

相关文章:

javascript - 是否可以使用 Xamarin.iOS 将字符串从 C# 返回到 Javascript?

c# - Windows 10 UWP 上的应用内购买问题

c# - protobuf-net 继承 : derived class hides base class property

c# - 使用泛型创建自定义控件

c++ - sqlConnection->Open() 不起作用

php - Mysql 使用多个不同的值组合进行搜索

php - 查询不会将用户名插入数据库

sql - 主键总是聚集的吗?

mysql - 查找一个月有多少分钟 : MySQL

sql - 在 sql 中选择将表与其自身连接的不同对