2012-06-08 65 views
0

我正在使用SQL Server 2005,我想檢索存儲在數據庫中的特定記錄的日期&想要將該日期設置爲datechooser。我怎樣才能做到這一點?如何從數據庫中檢索日期並將其綁定到datechooser?

+2

你嘗試過這麼遠嗎?你使用的是什麼數據庫框架(ADO.NET,LINQ to SQL,EF,...)?您使用的是什麼GUI框架(WinForms,WPF,Silverlight,...)? –

+0

你的DateChooser從哪裏來?什麼UI框架等? – abatishchev

回答

0

如何

mytextbox.Text = Result_returned_from_Execute_Scalar(); 
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... 
    } 
} 
+0

您已經發布了半功能代碼;) – abatishchev

+0

這不應該是完整的解決方案。只是一個暗示的方式來做到這一點。好吧,我忘了打開連接...對不起^^ – Tomtom

+0

並關閉它在結束:) – abatishchev

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(); 
相關問題