■ IDataReader 인터페이스에서 레코드를 객체로 변환하는 방법을 보여준다.
▶ Sample.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
namespace TestProject { /// <summary> /// 샘플 /// </summary> public class Sample { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public long ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get; set; } #endregion #region 점수 - Score /// <summary> /// 점수 /// </summary> public long Score { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return $"ID={ID}|성명={Name}|점수={Score}"; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
using System.Data; using System.Data.SQLite; using System.Reflection; namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 리스트 구하기 - GetList<TData>(reader) /// <summary> /// 리스트 구하기 /// </summary> /// <typeparam name="TData">데이터 타입</typeparam> /// <param name="reader">데이터 리더</param> /// <returns>리스트</returns> private static List<TData> GetList<TData>(IDataReader reader) where TData : new() { List<TData> list = new List<TData>(); while(reader.Read()) { TData data = new TData(); for(int i = 0; i < reader.FieldCount; i++) { string fieldName = reader.GetName(i); object value = reader.GetValue(i); PropertyInfo propertyInfo = data.GetType().GetProperty(fieldName); if(propertyInfo != null && value != DBNull.Value) { propertyInfo.SetValue(data, value); } } list.Add(data); } return list; } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { 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()) { List<Sample> sampleList = GetList<Sample>(reader); foreach(Sample sample in sampleList) { Console.WriteLine(sample); } } } } #endregion } |