简体   繁体   English

从数据库中检索日期值并将其分配给字符串变量 C#

[英]Retrieve date value from database and assign it in to string variable C#

I'm trying to get the date from my database and stores it in a variable here 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();
 chkDate_AM = reader2["timeOut_AM"].ToString("dd/MM/yyyy") ;

Assuming the DataType of "timeOut_AM" from SQL is DateTime you can do a convert in the reader.假设 SQL 中“timeOut_AM”的 DataType 是 DateTime,您可以在阅读器中进行转换。 Note you assuming it is a DateTime from SQL and that the value will never be null.请注意,您假设它是来自 SQL 的 DateTime,并且该值永远不会是 null。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM