DataTable dt = new DataTable("Student");//DataTable对象,表名为Student //开始增加列头 dt.Columns.Add("编号",typeof(Int32)); dt.Columns.Add("姓名",typeof(String)); dt.Columns.Add("成绩",typeof(String)); //开始增加行数据 DataRow row = dt.NewRow(); row[0] = 1; row[1] = "张"; row[2] = "98"; dt.Rows.Add(row);//向DataTable增加第一行记录 row = dt.NewRow(); row[0] = 2; row[1] = "李"; row[2] = "78"; dt.Rows.Add(row);//向DataTable增加第二行记录 //设置DataTable的主键 dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
转载于:https://www.cnblogs.com/lijialong/archive/2010/07/03/CreateDataTable.html