0

我正在使用 SQL Server 2005,我想检索存储在数据库中的特定记录的日期,并希望将该日期设置为 datechooser。我怎样才能做到这一点?

4

3 回答 3

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();
于 2012-06-08T07:36:37.447 回答
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...
    }
}
于 2012-06-08T06:54:21.987 回答
0

怎么样

mytextbox.Text = Result_returned_from_Execute_Scalar();
于 2012-06-08T06:50:14.077 回答