zoukankan      html  css  js  c++  java
  • C#数据之DataTable

    C#创建DataTable的几种方式

    第一种方式:直接添加数据对象

                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无参构造方法创建列,然后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);

    第三种方式:用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);     
  • 相关阅读:
    Java之基于注解的Excel导出
    从box-sizing:border-box属性入手,来了解盒模型
    提交代码出现 Push to origin/master was rejected 错误解决方法
    Mysql锁表解锁
    httpclient工具使用(org.apache.httpcomponents.httpclient)
    Jackson总结:常用注解、整合spring、自定义JsonSerializer
    Linux创建目录和文件的默认权限设置(umask命令)
    Apache Phoenix的子查询
    Apache Phoenix的Join操作和优化
    Apache Phoenix的Array类型
  • 原文地址:https://www.cnblogs.com/code1992/p/10249934.html
Copyright © 2011-2022 走看看