rockyashkumar
rockyashkumar

Reputation: 1332

how to compare the time and date using mysql and c#

I have a table logout with columns

logout_id
logout_datetime  values format like this(2011-09-08 11:09:09)

and i want to compare the logout_datetime with today date and time also....

for that i have done like this....

string dtStartString = DateTime.Today.ToString("yyyy-MM-dd");
string timeonly = DateTime.Now.ToShortTimeString();

string sql = @"select logout_id"; 
sql+= string.Format("where logout_datetime = '{0}'",dtStartString );

but it will compares only date in logoutdatetime ,its wrong i want to compare the time also

how can i do this..

would any one pls help on this...

Upvotes: 1

Views: 2077

Answers (3)

KV Prajapati
KV Prajapati

Reputation: 94625

Use Parameters.

string sql = "select logout_id from TableName 
                  where logout_datetime=@logout_datetime";
...
cmd.Parameters.Add("@logout_datetime",MySql.Data.MySqlClient.MySqlDbType.Datetime)
                   .Value=DateTime.Now; // you may assign any value of DateTime  type.

Upvotes: 1

You could use the date-format, or extract function.

You should read more about date time functions

Upvotes: 0

Davide Piras
Davide Piras

Reputation: 44595

Of course your code is only comparing the date because you are passing only dtStartString which does not have a time part.

try to pass this:

string dtStartString = DateTime.Now.ToString("yyyy-MM-dd  HH:mm:ss");

Upvotes: 3

Related Questions