C#创建DataTable的几种方式
2021-02-17 11:16
标签:ack data table tar type 方法 ring 三种方式 com 第一种方式:用DataColumn无参构造方法创建列,然后New每行列的数据 第二种方式:直接添加数据对象 第三种方式:用DataColumn有参构造方法创建列 C#创建DataTable的几种方式 标签:ack data table tar type 方法 ring 三种方式 com 原文地址:https://www.cnblogs.com/guhun/p/8378923.html DataTable table = new DataTable();
DataColumn strNameColumn = new DataColumn();//创建table的第一列
strNameColumn.DataType = System.Type.GetType("System.String");//该列的数据类型
strNameColumn.ColumnName = "strName";//该列得名称
DataColumn strSexColumn = new DataColumn();
strSexColumn.DataType = System.Type.GetType("System.String");
strSexColumn.ColumnName = "strSex";
DataColumn strEmailColumn = new DataColumn();
strEmailColumn.DataType = System.Type.GetType("System.String");
strEmailColumn.ColumnName = "strEmail";
// 将所有的列添加到table上
table.Columns.Add(strNameColumn);
table.Columns.Add(strSexColumn);
table.Columns.Add(strEmailColumn);
//添加数据
DataRow rowData = table.NewRow();
rowData["strName"] = "Tom";
rowData["strSex"] = "男";
rowData["strEmail"] = "Tom@atguigu.com";
table.Rows.Add(rowData);
rowData = table.NewRow();
rowData["strName"] = "Lucy";
rowData["strSex"] = "女";
rowData["strEmail"] = "Lucy@atguigu.com";
table.Rows.Add(rowData);
rowData = table.NewRow();
rowData["strName"] = "Jack";
rowData["strSex"] = "男";
rowData["strEmail"] = "Jack@atguigu.com";
table.Rows.Add(rowData);
DataTable table = new DataTable();
table.Columns.Add("strName", Type.GetType("System.String"));
table.Columns.Add("strSex", Type.GetType("System.String"));
table.Columns.Add("strEmail", Type.GetType("System.String"));
table.Rows.Add(new object[] { "Tom", "男", "Tom@atguigu.com" });
table.Rows.Add(new object[] { "Lucy", "女", "Lucy@atguigu.com" });
table.Rows.Add(new object[] { "Jack", "男", "Jack@atguigu.com" });
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("strName", Type.GetType("System.String"));
DataColumn dc2 = new DataColumn("strSex", Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("strEmail", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
//添加三条数据
DataRow row = dt.NewRow();
row["strName"] = "Tom";
row["strSex"] = "男";
row["strEmail"] = "Tom@atguigu.com";
dt.Rows.Add(row);
row = dt.NewRow();
row["strName"] = "Lucy";
row["strSex"] = "女";
row["strEmail"] = "Lucy@atguigu.com";
dt.Rows.Add(row);
row = dt.NewRow();
row["strName"] = "Jack";
row["strSex"] = "男";
row["strEmail"] = "Jack@atguigu.com";
dt.Rows.Add(row);
文章标题:C#创建DataTable的几种方式
文章链接:http://soscw.com/index.php/essay/56538.html