1. 程式人生 > >把資料庫中的datatime型別欄位 用字串的形式輸出(如05-20-2012)

把資料庫中的datatime型別欄位 用字串的形式輸出(如05-20-2012)

SqlConnection con = new SqlConnection("server=localhost;database=bookmanage;uid=sa;pwd=1234");
        con.Open();
        string sql = "SELECT ID,CONVERT(VARCHAR(10),nowdate ,110) as 時間 FROM InsertTime where ID=4";

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter(sql,con);

        da.Fill(ds, "table");

        TextBox2.Text = ds.Tables[0].Rows[0]["時間"].ToString();


其中  nowdate  為 資料庫中欄位 其型別為 datatime

SELECT ID,CONVERT(VARCHAR(10),nowdate ,110) as 時間 FROM InsertTime