• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
yoxking
yoxking Tech.
博客园    首页    新随笔    联系   管理    订阅  订阅

将Collection对象(objects)或数组(arrays)转换为 DataTables

Converting objects or arrays of objects to DataTables
Recently there have been some articles about not using DataSets or DataTables in web

applications. Scott Mitchell has Why I Don't Use DataSets in My ASP.NET Applications and

More On Why I Don't Use DataSets in My ASP.NET Applications. Karl Sequin has On the Way to

Mastering ASP.NET: Introducing Custom Entity Classes.

I prefer to use custom objects also, mostly because I like to abstract the database away

from the client app. So I have this API with custom classes (fed from DataReaders) and a

coworker wants to use my API to drive some reports. The only problem is the tool they are

using doesn't work with arrays of objects. They say it needs DataSets, DataTables or

DataReaders.

At first I started getting nervous but then I thought about it. DataSets and DataTables are

fed from DataReaders. That's when I said to myself, "Self, you can feed DataSets and

DataTables from your custom classes by using reflection."

Now I am far from a Reflection guru, but all they need are column names,  data types and

values. I can build DataColumns from the property info of classes dynamically using

Reflection. The following code is my first pass. If anyone sees a way to improve on this

then by all means, let's hear it.

using System;

using System.Data;

using System.Reflection;

using System.Reflection.Emit;

 

namespace ObjToAdo

{

    /// <summary>

    /// Summary description for Converter.

    /// </summary>

    public class Converter

    {

      private Converter()   {}

 

      /// <summary>

      ///

      /// </summary>

      /// <param name="o"></param>

      /// <returns></returns>

      public static DataTable ConvertToDataTable(Object o)

      {  

        PropertyInfo[] properties = o.GetType().GetProperties();

        DataTable dt = CreateDataTable(properties);

        FillData(properties, dt, o);

        return dt;

      }

 

      /// <summary>

      ///

      /// </summary>

      /// <param name="o"></param>

      /// <returns></returns>

      public static DataTable ConvertToDataTable(Object[] array)

      {

        PropertyInfo[] properties = array[0].GetType().GetElementType

().GetProperties();

        DataTable dt = CreateDataTable(properties);

 

        if (array.Length != 0)

        {

            foreach(object o in array)

              FillData(properties, dt, o);

 

        }

 

        return dt;

      }

 

      /// <summary>

      ///

      /// </summary>

      /// <param name="properties"></param>

      /// <returns></returns>

      private static DataTable CreateDataTable(PropertyInfo[] properties)

      {

        DataTable dt = new DataTable();

        DataColumn dc = null;

 

        foreach(PropertyInfo pi in properties)

        {

            dc = new DataColumn();

            dc.ColumnName = pi.Name;

            dc.DataType = pi.PropertyType;

           

            dt.Columns.Add(dc);          

        }

 

        return dt;

      }

 

 

      /// <summary>

      ///

      /// </summary>

      /// <param name="properties"></param>

      /// <param name="dt"></param>

      /// <param name="o"></param>

      private static void FillData(PropertyInfo[] properties, DataTable dt, Object

o)

      {

        DataRow dr = dt.NewRow();

 

        foreach(PropertyInfo pi in properties)

            dr[pi.Name] = pi.GetValue(o, null);

 

        dt.Rows.Add(dr);   

      }

    }

}

 

以上为原文,但测试后有点问题,下面为修改的部分代码.

 


 public static DataTable ConvertToDataTable(Object[] array)
        {

            PropertyInfo[] properties = array[0].GetType().GetProperties();

            DataTable dt = CreateDataTable(properties);

            if (array.Length != 0)
            {
                foreach (object o in array)

                    FillData(properties, dt, o);
            }

            return dt;

        }

 

 

posted @ 2008-03-08 11:49  yoxking   阅读(411)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3