C#创建DataTable的几种方式

2021-02-17 11:16

阅读:458

标签:ack   data   table   tar   type   方法   ring   三种方式   com   

第一种方式:用DataColumn无参构造方法创建列,然后New每行列的数据    

           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" });

第三种方式:用DataColumn有参构造方法创建列

            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的几种方式

标签:ack   data   table   tar   type   方法   ring   三种方式   com   

原文地址:https://www.cnblogs.com/guhun/p/8378923.html


评论


亲,登录后才可以留言!