Home > Net >  Retrieve date value from database and assign it in to string variable C#
Retrieve date value from database and assign it in to string variable C#

Time:08-16

I'm trying to get the date from my database and stores it in a variablehere is the data from my table

This is my current code but this take time and date

SqlCommand command2 = new SqlCommand("SELECT timeOut_AM FROM TimeOut_AM WHERE FORMAT(timeOut_AM, 'yyyy/MM/dd') = CONVERT (DATE, SYSDATETIME());", conn);

SqlDataReader reader2 = null;

conn.Open();
reader2 = command2.ExecuteReader();

while (reader2.Read())
{
    chkDate_AM = Convert.ToString(reader2["timeOut_AM"]);
}

conn.Close();

CodePudding user response:

 chkDate_AM = reader2["timeOut_AM"].ToString("dd/MM/yyyy") ;

CodePudding user response:

Assuming the DataType of "timeOut_AM" from SQL is DateTime you can do a convert in the reader. Note you assuming it is a DateTime from SQL and that the value will never be null.

chkDate_AM = Convert.ToDateTime(reader["timeOut_AM"]).ToString("dd/MM/yyyy");
  • Related