网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

DotNet中的DataTable相关操作

 1.将泛型集合类转换成DataTable(表中无数据时使用):

十年的谯城网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整谯城建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“谯城网站设计”,“谯城网站推广”以来,每个客户项目都认真落实执行。

public static DataTable NullListToDataTable(IList list)
        {
            var result = new DataTable();
            if (list.Count <= 0) return result;
            var propertys = list[0].GetType().GetProperties();
            foreach (var pi in propertys)
            {
                if (pi != null)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }
            }
            for (var i = 0; i < list.Count; i++)
            {
                var tempList = new ArrayList();
                foreach (var pi in propertys)
                {
                    var obj = pi.GetValue(list[i], null);
                    tempList.Add(obj);
                }
                var array = tempList.ToArray();
                result.LoadDataRow(array, true);
            }
            return result;
        }

  2.将泛型集合类转换成DataTable(表中有数据时使用):

public static DataTable NoNullListToDataTable(IList list)
        {
            var ds = new DataSet();
            var dt = new DataTable(typeof(T).Name);
            var myPropertyInfo =
                typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var t in list)
            {
                if (t == null) continue;
                var row = dt.NewRow();
                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    var pi = myPropertyInfo[i];
                    var name = pi.Name;
                    if (dt.Columns[name] != null) continue;
                    DataColumn column;
                    if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")
                    {
                        column = new DataColumn(name, typeof(int));
                        dt.Columns.Add(column);
                        if (pi.GetValue(t, null) != null)
                            row[name] = pi.GetValue(t, null);
                        else
                            row[name] = DBNull.Value;
                    }
                    else
                    {
                        column = new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                        row[name] = pi.GetValue(t, null);
                    }
                }
                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            return ds.Tables[0];
        }

3.表中有数据或无数据时使用,可排除DATASET不支持System.Nullable错误:

public static DataTable ToDataTable(IList list)
        {
            if (list == null || list.Count <= 0)
            {
                var result = new DataTable();
                object temp;
                if (list == null || list.Count <= 0) return result;
                var propertys = list[0].GetType().GetProperties();
                foreach (var pi in propertys)
                {
                    {
                        result.Columns.Add(pi.Name, pi.PropertyType);
                    }
                }
                for (var i = 0; i < list.Count; i++)
                {
                    var tempList = new ArrayList();
                    foreach (var pi in propertys)
                    {
                        var obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    var array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
                return result;
            }
            var ds = new DataSet();
            var dt = new DataTable(typeof(T).Name);
            var myPropertyInfo =
                typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var t in list)
            {
                if (t == null) continue;
                var row = dt.NewRow();
                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    var pi = myPropertyInfo[i];
                    var name = pi.Name;
                    if (dt.Columns[name] != null) continue;
                    DataColumn column;
                    if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")
                    {
                        column = new DataColumn(name, typeof(int));
                        dt.Columns.Add(column);
                        if (pi.GetValue(t, null) != null)
                            row[name] = pi.GetValue(t, null);
                        else
                            row[name] = DBNull.Value;
                    }
                    else
                    {
                        column = new DataColumn(name, pi.PropertyType);
                        dt.Columns.Add(column);
                        row[name] = pi.GetValue(t, null);
                    }
                }
                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            return ds.Tables[0];
        }

4.合并相同的DataTable:

public static DataTable MergeSameDatatable(DataTable dataTable1, DataTable dataTable2)
        {
            var newDataTable = dataTable1.Clone();
            var obj = new object[newDataTable.Columns.Count];
            for (var i = 0; i < dataTable1.Rows.Count; i++)
            {
                dataTable1.Rows[i].ItemArray.CopyTo(obj, 0);
                newDataTable.Rows.Add(obj);
            }
            for (var i = 0; i < dataTable2.Rows.Count; i++)
            {
                dataTable2.Rows[i].ItemArray.CopyTo(obj, 0);
                newDataTable.Rows.Add(obj);
            }
            return new DataTable();
        }

5.将两个列不同的DataTable合并成一个新的DataTable :

public static DataTable UniteDataTable(DataTable dt1, DataTable dt2, string dtName)
        {
            var dt3 = dt1.Clone();
            for (var i = 0; i < dt2.Columns.Count; i++)
            {
                dt3.Columns.Add(dt2.Columns[i].ColumnName);
            }
            var obj = new object[dt3.Columns.Count];

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                dt1.Rows[i].ItemArray.CopyTo(obj, 0);
                dt3.Rows.Add(obj);
            }

            if (dt1.Rows.Count >= dt2.Rows.Count)
            {
                for (var i = 0; i < dt2.Rows.Count; i++)
                {
                    for (var j = 0; j < dt2.Columns.Count; j++)
                    {
                        dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
                    }
                }
            }
            else
            {
                for (var i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++)
                {
                    var dr3 = dt3.NewRow();
                    dt3.Rows.Add(dr3);
                }
                for (var i = 0; i < dt2.Rows.Count; i++)
                {
                    for (var j = 0; j < dt2.Columns.Count; j++)
                    {
                        dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();
                    }
                }
            }
            dt3.TableName = dtName;
            return dt3;
        }

6.Datatable 转 List

public static List> DataTableToListDictory(DataTable table)
        {
            var ld = new List>();
            for (var i = 0; i < table.Rows.Count; i++)
            {
                var dic = new Dictionary();
                for (var j = 0; j < table.Columns.Count; j++)
                {
                    dic.Add(table.Columns[j].ColumnName, table.Rows[i][j]);
                }
                ld.Add(dic);
            }
            return ld;
        }

新闻名称:DotNet中的DataTable相关操作
本文URL:http://bjjierui.cn/article/gjdece.html

其他资讯