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

■ IDataReader 인터페이스 : 레코드를 객체로 변환하기

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


TestProject.zip


DataReaderExtension.cs

 

 

using System;

using System.Collections.Generic;

using System.Data;

 

namespace TestProject

{

    /// <summary>

    /// 데이터 리더 확장

    /// </summary>

    public static class DataReaderExtension

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

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

 

        /// <summary>

        /// 객체 구하기

        /// </summary>

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

        /// <param name="reader">데이터 리더 인터페이스</param>

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

        public static TObject GetObject<TObject>(this IDataReader reader)

        {

            TObject newObject = (TObject)Activator.CreateInstance(typeof(TObject), null);

        

            TypeAccessor accessor = TypeAccessor.Create(newObject.GetType());

 

            Dictionary<string, Member> dictionary = new Dictionary<string, Member>();

 

            foreach(Member member in accessor.GetMembers())

            {

                dictionary.Add(member.Name, member);

            }

 

            for(int i = 0; i < reader.FieldCount; i++)

            {

                if(dictionary.ContainsKey(reader.GetName(i)))

                {

                    accessor[newObject, reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);

                }

            }

 

            return newObject;

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

using System.Data;

using System.Data.SQLite;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.Title = "IDataReader 인터페이스 : 레코드를 객체로 변환하기";

 

            string connectionString = "URI=file:test.db";

 

            using(SQLiteConnection connection = new SQLiteConnection(connectionString))

            {

                connection.Open();

 

                SQLiteCommand command = new SQLiteCommand();

 

                command.Connection  = connection;

                command.CommandType = CommandType.Text;

                command.CommandText = "SELECT * FROM Sample";

 

                using(SQLiteDataReader reader = command.ExecuteReader())

                {

                    while(reader.Read())

                    {

                        Sample sample = reader.GetObject<Sample>();

 

                        Console.WriteLine(sample);

                    }

                }

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요