첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ DataRow 클래스 : 데이터 행에서 객체 구하기 예제

------------------------------------------------------------------------------------------------------------------------

using System;

using System.Reflection;

using System.Data;

 

/// <summary>

/// 직원

/// </summary>

public class Employee

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region ID - ID

 

    /// <summary>

    /// ID

    /// </summary>

    public int ID { get; set; }

 

    #endregion

    #region 성명 - Name

 

    /// <summary>

    /// 성명

    /// </summary>

    public string Name { get; set; }

 

    #endregion

}

 

DataTable table = new DataTable();

 

table.Columns.Add("ID"  , typeof(int   ));

table.Columns.Add("Name", typeof(string));

 

table.Rows.Add(1, "김철수");

table.Rows.Add(2, "이철희");

table.Rows.Add(3, "홍길동");

table.Rows.Add(4, "이미영");

table.Rows.Add(5, "이명숙");

 

table.AcceptChanges();

 

Employee employee = GetObject<Employee>(table.Rows[0]);

 

Console.WriteLine(employee.ID  );

Console.WriteLine(employee.Name);

------------------------------------------------------------------------------------------------------------------------

 

■ DataRow 클래스 : 객체 구하기

------------------------------------------------------------------------------------------------------------------------

using System;

using System.Reflection;

using System.Data;

 

#region 객체 구하기 - GetObject<T>(sourceRow)

 

/// <summary>

/// 객체 구하기

/// </summary>

/// <typeparam name="T">객체 타입</typeparam>

/// <param name="sourceRow">소스 행</param>

/// <returns>객체</returns>

public T GetObject<T>(DataRow sourceRow) where T : new()  

{

    Type targetType = typeof(T);

 

    T targetObject = new T();

 

    foreach(DataColumn sourceColumn in sourceRow.Table.Columns)

    {

        string columnName = sourceColumn.ColumnName;

 

        PropertyInfo propertyInfo = targetType.GetProperty

        (

            columnName.ToLower(),

            BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance

        );

 

        if(propertyInfo != null)

        {

            object columnValue = sourceRow[columnName];

 

            bool isNullable = (Nullable.GetUnderlyingType(propertyInfo.PropertyType) != null);

 

            if(isNullable)

            {

                if(columnValue is DBNull)

                {

                    columnValue = null;

                }

                else

                {

                    columnValue = Convert.ChangeType

                    (

                        columnValue,

                        Nullable.GetUnderlyingType(propertyInfo.PropertyType)

                    );

                }

            }

            else

            {

                if(columnValue is DBNull)

                {

                    columnValue = null;

                }

                else

                {

                    columnValue = Convert.ChangeType(columnValue, propertyInfo.PropertyType);

                }

            }

 

            propertyInfo.SetValue(targetObject, columnValue, null);

        }

    }

 

    return targetObject;  

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요