You're going wrong by trying to include a value directly in your SQL query to start with. Use parameterized SQL and specify the value as a DateTime, and assuming your database table is also using a date/datetime field (which it should be), you should be fine.
You should avoid including values directly in your SQL for three reasons:
- You'll end up with troublesome string conversions which can use inappropriate formats etc. That's the problem here.
- You're inviting SQL injection attacks
- You're mixing code and data, which makes it harder to read the code and harder to check the data
You want 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 isn't already a suitable data type, you should change that...