You are mistaken in trying to include the value directly in your SQL query to get started. Use parameterized SQL and specify the value as DateTime , and if your database table also uses the date / datetime field (this should be), you should be fine.
You should avoid including values ββdirectly in your SQL for three reasons:
- As a result, you will encounter unpleasant string conversions that may use inappropriate formats, etc. This is the problem here.
- You invite SQL injection attacks
- You mix code and data, which makes it hard to read code and harder to check data.
You need something like:
string sql = @"Select distinct v.* from Ven v inner join Des d on v.venid=d.venid and cast(d.despdate as datetime) between @start and @end"; using (MySqlCommand command = new MySqlCommand(connection, sql)) { command.Parameters.AddWithValue("@start", startDateTime); command.Parameters.AddWithValue("@end", endDateTime); // Execute the command here }
If Des.despdate not a suitable data type, you should change this ...
source share