C#创建DataTable的语法

namespace EazyCMS.Web.admin
{
    public partial class ceshi1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(connStr))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        adapter.Fill(dt);
                    }
                }
            }
            //以上代码得到一个有数据的DataTable实例dt
            DataTable dtN = new DataTable();
            dtN.Columns.Add("ID");
            dtN.Columns.Add("Name");

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow tr = dtN.NewRow();
                    tr["ID"] = dt.Rows[i]["id"].ToString();
                    tr["Name"] = dt.Rows[i]["siteid"].ToString() + "-" + dt.Rows[i]["Name"].ToString();
                    dtN.Rows.Add(tr);
                }
                Repeater1.DataSource = dtN;
                Repeater1.DataBind();
            }

        }
    }
}

 

posted @ 2016-11-10 15:18  雅比斯  阅读(2053)  评论(0编辑  收藏  举报