Can anyone help me out with this query i m trying to execute,
static public DataTable GetAllCustomers()
{
string sql = "Select * from [project] where [condition] = 0 AND [Time] < '" + DateTime.Now + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
It returns nothing
Any ideas where i am getting the query wrong.
Putting DateTime’s into SQL Strings is a recipe for disaster thanks to formatting and locations. Change your sql line to:
This will use the servers own current date time in whatever format it is expecting.
If you absolutely need to do things client side then use SqlCommand and instead of adding DateTime as a string put a “Time < @” and then add the DateTime as a command parameter. That will avoid formatting problems at least.