DataTable与数组相互转换

来源:互联网 发布:java二分法递归 编辑:程序博客网 时间:2024/05/16 17:25

原文地址:http://hi.baidu.com/fxb_hyb/blog/item/ef80aba25e8e60a5cbefd0de.html

using System;
using System.Data;
using System.Data.OleDb;

namespace pxkt_datatable
{
class pxkt
{
   public pxkt()
   {
    string[] g = new string[10];
    for(int i=0;i<10;i++)
    {
     g[i]=i.ToString();
    }

    //    数组 转 数据表DataTable
    DataTable dt = new DataTable();
    dt.Columns.Add("name",typeof(string));
    for (int k=0;k<g.Length;k++)
    {
     DataRow dr = dt.NewRow();
     dr["name"]=g[k];
     dt.Rows.Add(dr);
    }

    //输出数据表
    Console.WriteLine("打出数据表!");
    for(int j= 0;j<dt.Rows.Count;j++)
    {
     DataRow dr1 = dt.Rows[j];
     Console.WriteLine(dr1["name"].ToString());
    }
   
    //数据表DataTable转数组
    string[] arrayA = new string[dt.Rows.Count];
    for(int x=0;x<dt.Rows.Count;x++)
    {
     DataRow dr2 = dt.Rows[x];
     arrayA[x]=Convert.ToString(dr2["name"]);
    }

    //输出数组
    Console.WriteLine("打出数组!");
    for(int y=0;y<arrayA.Length;y++)
    {
     Console.WriteLine(arrayA[y]);
    }

    foreach(string printarray in arrayA)
    {
     Console.WriteLine(printarray);
    }
   }

   static pxkt()
   {
       
   }
}

class test

{
   static void Main()
   {
    pxkt p = new pxkt();
   }
}
}

来自:http://www.cnblogs.com/peirunou/archive/2008/12/04/1347753.html

 

DataTable转数组

 

        /// <summary>
        /// DataTable转换为一维字符串数组
        /// </summary>
        /// <returns></returns>
        public static string[] dtToArr1(DataTable dt) {
            if (dt == null || dt.Rows.Count == 0) return new string[0];
            string[] sr = new string[dt.Rows.Count];
            for(int i=0;i<dt.Rows.Count;i++){
                if (Convert.IsDBNull(dt.Rows[i][0])) sr[i] = "";
                else sr[i] = dt.Rows[i][0] + "";
            }
            return sr;
        }

 

来自:http://hi.baidu.com/qingcaodeng13/blog/item/633eb20941f8a5a92fddd4c2.html

 

int row=DataTable1.Rows.Count;
int col=DataTable1.Columns.Count;
int[,] tb=new int[row,col];
for(int r=0;r<row;r++)
{
for(int c=0;c<col;c++)
{
tb[r,c]=int.Parse(DataTable1.Rows[r][c].ToString());
}
}
DataTable1是DataTable的一个实例,tb为所得的二维数组

来自:http://zhidao.baidu.com/question/32876214.html

在最近做的一个案子里,需要绑定实体数组比如Materiel[]绑定到界面(winform/webform都有),虽然可以直接绑定数组到GridView,但排序,过滤,查找等操作在数组里不是很方便。所以想借用DataTable做数据源。

最简单的方法就是手动建一个DataTable。为每个Materiel的property建一个Column,然后指明其数据类型。建好Table之后,循环为每个Materiel创建一个新行。如果多有几个界面,虽然做起来都差不多,但代码很难重用。

另外数据都是从WebService获取,form不允许直接访问DB,所以也不能通过ADO.net获取DataTable。

经过一段时间的考量后决定见一个专用的Utility类EntityCollectionsConvert,接口为

DataTable ToDataTable(object[] entitys);

DataTable ToDataTable<T>(List<T> entitys)

实现的原理也比较简单

1.将判断entitys不为空;

2.取出entitys的所有property

3.在DataTable中为每个property添加一列(包括元素类型)

4.为每个entity添加一行。

5.自动生成单元测试,测试,添加到项目中

view plaincopy to clipboardprint?
  1. public static DataTable ToDataTable<T>(List<T> entitys)
  2. {
  3. 3
  4. //检查实体集合不能为空
  5. if (entitys ==null|| entitys.Count<1)
  6. {
  7. throw new Exception("需转换的集合为空");
  8. }
  9. 9
  10. 10 //取出第一个实体的所有Propertie
  11. 11 Type entityType= entitys[0].GetType();
  12. 12 PropertyInfo[] entityProperties= entityType.GetProperties();
  13. 13
  14. 14 //生成DataTable的structure
  15. 15 //生产代码中,应将生成的DataTable结构Cache起来,此处略
  16. 16 DataTable dt new DataTable();
  17. 17 for (int 0; entityProperties.Length; i++)
  18. 18 {
  19. 19 dt.Columns.Add(entityProperties[i].Name,entityProperties[i].PropertyType);
  20. 20 }
  21. 21
  22. 22 //将所有entity添加到DataTable中
  23. 23 foreach (object entity in entitys)
  24. 24 {
  25. 25 //检查所有的的实体都为同一类型
  26. 26 if (entity.GetType()!=entityType)
  27. 27 {
  28. 28 throw new Exception("要转换的集合元素类型不一致");
  29. 29 }
  30. 30 object[] entityValues new object[entityProperties.Length];
  31. 31 for (int 0; entityProperties.Length; i++)
  32. 32 {
  33. 33 entityValues[i] entityProperties[i].GetValue(entity, null);
  34. 34
  35. 35 }
  36. 36 dt.Rows.Add(entityValues);
  37. 37 }
  38. 38 return dt;
  39. 39 }
0 0
原创粉丝点击