如何从数据库中检索日期&将它绑定到datechooser?

时间:2012-06-08 06:49:13

标签: c# sql-server sql-server-2005 ado.net

我正在使用SQL Server 2005,我想检索存储在数据库中的日期以获取特定记录和想要将该日期设置为datechooser。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

using (SqlConnection connection= new SqlConnection("CONNECTIONSTRING");
using (SqlCommand command = new SqlCommand("SELECT-QUERY", connection))
{
    //set parameters if you need them
    //command.Parameters.AddWithValue("@paramName", paramValue);

    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    if(reader.Read())
    {
         object date = reader["COLUMNNAME"];
         //Do something with the date...
    }
}

答案 1 :(得分:1)

System.Windows.Forms.DateTimePicker dateTimePicker1 = new System.Windows.Forms.DateTimePicker();            
dateTimePicker1.Name = "dateTimePicker1";
dateTimePicker1.Size = new System.Drawing.Size(200, 20);            

SqlConnection connection= new SqlConnection(@"Data Source=DATASOURCE;Initial Catalog=DBNAME;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD"); 
using (SqlCommand command = new SqlCommand("SELECT Top 1 colDate From tblTemp", connection)) 
{     
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();     
    if (reader.Read())     
    {          
        DateTime dt=new DateTime();
        dt=DateTime.Now;
        bool b=DateTime.TryParse(reader["CreatedAt"].ToString(),out dt);
        dateTimePicker1.Value = dt;
    }

    connection.Close();

答案 2 :(得分:0)

怎么样

mytextbox.Text = Result_returned_from_Execute_Scalar();