首页 > 代码库 > Datatable用法

Datatable用法

           //Datatable 的用法
           /*
            * 1、基本的DataTable列行增加
            * 2、排序、筛选等用法
            * 3、xml之间的转化
            * 4、Json之间的转换
            * 5、list之间的转换
            * 6、实体类之间的转换
            */

今天把常用的Datatable整理了一下,虽然平时经常用这个,但是没有总结过,这次刚好有点时间做个笔记;

先看第一个:

 DataTable dt = new DataTable();
            dt.Columns.Add("ID",typeof(int));
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add("Sex",typeof(string));
            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Name"] = "张三";
            dr["Sex"] = "";
            dt.Rows.Add(dr);
             dr = dt.NewRow();
            dr["ID"] = "2";
            dr["Name"] = "李四";
            dr["Sex"] = "";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "12";
            dr["Name"] = "旺旺";
            dr["Sex"] = "";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "22";
            dr["Name"] = "王五";
            dr["Sex"] = "";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ID"] = "32";
            dr["Name"] = "赵六";
            dr["Sex"] = "";
            dt.Rows.Add(dr);

第二个:按照其中已知的某一列排序(根据实际列的类型需要修改代码)

 static  public DataTable DtSortA(DataTable Dt, string column)
        {
            DataTable dtNew = Dt.Clone();
            dtNew.Columns[column].DataType = typeof(DateTime);
            foreach (DataRow s in Dt.Rows)
            {
                dtNew.ImportRow(s);//导入旧数据
            }
            dtNew.DefaultView.Sort = column + " desc";
            dtNew = dtNew.DefaultView.ToTable();
            return dtNew;
        }
      static public DataTable DtSortB(DataTable Dt, string column)
        {
            DataTable dtNew = Dt.Clone();
            foreach (DataRow item in Dt.AsEnumerable().OrderByDescending(a => Convert.ToDateTime(a[column])))
            {
                dtNew.ImportRow(item);
            }
            return dtNew;
        }
            DataTable dt_order_soon = dt_invest.Clone();//克隆表

            if (dt_invest != null && dt_invest.Rows.Count > 0)
            {
                DataRow[] dt_soon = dt_invest.Select("1=1");//筛选条件
                for (int e = 0; e < dt_soon.Length; e++)
                {
                    var day = new JBLProCompute.InComeCount().ReturnDays(DateTime.Now, Convert.ToDateTime(dt_soon[e]["DateOver"].ToString()));
                    if (day >= 0 && day <= 7)
                    {
                        dt_order_soon.ImportRow(dt_soon[e]);
                    }
                }
}
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));//Type.GetType("System.String")
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add("Sex",typeof(string));
            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Name"] = "张三";
            dr["Sex"] = "";
            dt.Rows.Add(dr);
            DataTable ddt = new DataTable();
            //输出指定的列数据
           ddt= dt.DefaultView.ToTable(false,new string[] {"ID","Name"});

 

第三:

DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));//Type.GetType("System.String")
            dt.Columns.Add("Name",typeof(string));
            dt.Columns.Add("Sex",typeof(string));
            DataRow dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Name"] = "张三";
            dr["Sex"] = "";
            dt.Rows.Add(dr);////想要输出xml格式,当前的Datatable表必须要有表名
           dt.TableName = "myData";//添加Datatable表名才能输出xml格式
           System.IO.StringWriter dsw = new System.IO.StringWriter();
           System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(dsw);
           dt.WriteXml(xw);
           string s = dsw.ToString();
            //将xml格式转成Datatable格式,直接是xml格式的字符串最外层是根节点,第二层是表名,三层是字段和数据
           string strxml = @"<xml><Login><ID>1</ID><Name>小明</Name></Login><Login><ID>2</ID><Name>小万</Name></Login></xml>";
          //第二种是文件读入xml
          XmlDocument docddd=new XmlDocument();
docddd.Load(Server.MapPath(
"/datatable.xml"));
DataTable dt_xml
= ConvertXMLToDataSet(docddd.InnerXml).Tables[0];

 

Datatable用法