Reputation: 91
I try to compare these 2 dates in string format but it couldnt work out {
SqlCommand cmd =new SqlCommand($"SELECT Last_Login_Date FROM [dbo].[Member] WHERE EmailAddress='{emailAddress}');") ;
SqlDataReader reader = cmd.ExecuteReader();
string lld = reader["Last_Login_Date"].ToString();
string currentDate = DateTime.Now.ToString();if (DateTime.Parse(lld,currentDate))
}
Upvotes: 1
Views: 218
Reputation: 4260
You cannot use datefunctions on string. Read the date from datareader and add your comparision logic.
using (var conn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand($"SELECT Last_Login_Date FROM [dbo].[Member] WHERE EmailAddress='{emailAddress}';"))
{
DateTime lastlogindate;
cmd.Connection = conn;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
lastlogindate = reader.GetDateTime("Last_Login_Date");
//add your comparision logic here
Console.WriteLine(DateTime.Compare(lastlogindate, DateTime.Now)); //result -1
}
}
Upvotes: 0
Reputation: 137
Make sure data type of Last_Login_Date is datetime or date.
I'll use GetDateTime
to get value.
SQL Server SqlDataReader.GetDateTime
MySQL MySqlDataReader.GetDateTime
like this reader.GetDateTime(reader.GetOrdinal("ColumnName"));
So you will change your string lld
to DateTime lld
ex:DateTime lld = reader.GetDateTime(reader.GetOrdinal("ColumnName"));
Then compare two variables DateTime.Compare(currentDate,lld)
.
Hope this help !
Upvotes: 1