C#手动建立Table后,并设置列和赋值

C#手动建立Table后,并设置列和赋值

public DataTable CreateTable()        {            //创建Table1            DataTable dt = new DataTable();            //对Table1添加列名,并设置列值类型            DataTable dt1 = new DataTable();//创建Table1            dt1.Columns.Add("ID", typeof(string));            dt1.Columns.Add("NAME", typeof(string));            dt1.Columns.Add("AGE", typeof(int));            dt1.Columns.Add("SEX", typeof(string));            //对Table1进行设置KEY            dt1.PrimaryKey = new DataColumn[] { dt1.Columns["ID"] };            //对Tabel添加数值            for (int i = 0; i < 2; i++)            {                DataRow dr = dt1.NewRow();                dr["ID"] = "00" + i.ToString();                dr["NAME"] = "00-" + i.ToString();                dr["AGE"] = 15 + i;                dr["SEX"] = "M";                dt1.Rows.Add(dr);            }            return dt1;        }
(0)

相关推荐