如何在标签中显示日期

时间:2013-10-03 08:06:46

标签: c# asp.net sql asp.net-mvc

    using (SqlConnection conn = conMgr.GetDatabaseConnection())
    {
        using (SqlCommand cmd = new SqlCommand("getEdit", conn))
        {


            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@id", id));
            cmd.Connection = conn;
            using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) 
            { 
                  while (rdr.Read()) 
                     {
                        TextBox1.Text = rdr.GetInt32(rdr.GetOrdinal("id")).ToString(); 
                        TextBox2.Text = rdr.GetString(rdr.GetOrdinal("name"));
                        DropDownList4.SelectedValue = rdr.GetString(rdr.GetOrdinal("country"));
                        string dt = rdr.GetString(rdr.GetOrdinal("dob"));
                        Label14.Text = dt;




                     }  
                rdr.Close();
            }
        }

dob是日期时间类型。所以请告诉我如何使用标签显示它。如何将日期时间对象转换为字符串类型。

1 个答案:

答案 0 :(得分:5)

您需要使用GetDateTime,然后您可以使用ToShortDateString以dd / mm / yyyy的格式获取它:

string dt = rdr.GetDateTime(rdr.GetOrdinal("dob")).ToShortDateString();
Label14.Text = dt;