.net DateTable的用法

  SqlConnection con = new SqlConnection(constring);//是否打开连接池
        con.Open();
        string cmdText = "select Note.*,BackNote.* from Note inner join BackNote on Note.n_id=BackNote.n_id where Note.n_id='23'";//sql内联式
        SqlCommand cmd = new SqlCommand(cmdText, con);
        SqlDataReader sdr = cmd.ExecuteReader();
        //创建一个数据表 DataTable
        DataTable dt = new DataTable();//实例化一个表

        //数据表增加一列
        dt.Columns.Add("n_id",typeof(int));
        dt.Columns.Add("NoteTitle", typeof(string));
        dt.Columns.Add("NotePubtime", typeof(DateTime));
        dt.Columns.Add("BackTitle", typeof(string));
        dt.Columns.Add("BaclTime", typeof(DateTime));

        //数据库的约束对象 Constraints  保证 n_id NoteTitle 列 唯一键约束
        //UniqueConstraint ucst = new UniqueConstraint(new DataColumn[] { dt.Columns["n_id"], dt.Columns["NoteTitle"] } );
        if (sdr.Read())
        {
            //数据表增加一行
            DataRow dr = dt.NewRow();
            dr["n_id"] = sdr["n_id"].ToString();
            dr["NoteTitle"] = sdr["title"].ToString();
            dr["NotePubtime"] = sdr["pubtime"].ToString();
            dr["BackTitle"] = sdr["title"].ToString();
            dr["BaclTime"] = sdr["pubtime"].ToString();
            dt.Rows.Add(dr);
        }
        sdr.Close();
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();

你可能感兴趣的:(sql,.net)