■ IDbConnection 클래스의 QueryMultiple 확장 메소드를 사용해 2개 이상의 SELECT문을 조회하는 방법을 보여준다.
▶ CodeGroupModel.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 |
using System; namespace TestProject { /// <summary> /// 코드 그룹 모델 /// </summary> public class CodeGroupModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 명칭 - NAME /// <summary> /// 명칭 /// </summary> public string NAME { get; set; } #endregion } } |
▶ CodeModel.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 |
using System; namespace TestProject { /// <summary> /// 코드 모델 /// </summary> public class CodeModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 그룹 ID - GROUP_ID /// <summary> /// 그룹 ID /// </summary> public string GROUP_ID { get; set; } #endregion #region 명칭 - NAME /// <summary> /// 명칭 /// </summary> public string NAME { get; set; } #endregion #region 값 - VALUE /// <summary> /// 값 /// </summary> public string VALUE { get; set; } #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 |
using System; using System.Collections.Generic; using System.Linq; using Npgsql; using Dapper; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string sql = @" SELECT * FROM COM_CODE_GROUP WHERE ID = '5A9E697A-A154-4F21-85D6-258E457AF8DF'; SELECT * FROM COM_CODE WHERE GROUP_ID = '5A9E697A-A154-4F21-85D6-258E457AF8DF'; "; string connectionString = @"Host=127.0.0.1;Port=5432;Database=test;User ID=postgres;Password=postgres"; CodeGroupModel codeGroup = null; List<CodeModel> codeList = null; using(NpgsqlConnection connection = new NpgsqlConnection(connectionString)) { var reader = connection.QueryMultiple(sql); using(reader) { codeGroup = reader.ReadFirstOrDefault<CodeGroupModel>(); codeList = reader.Read<CodeModel>().ToList(); } } if(codeGroup != null) { Console.WriteLine("CODE GROUP NAME : {0}", codeGroup.NAME); } if(codeList != null && codeList.Count > 0) { foreach(CodeModel code in codeList) { Console.WriteLine(" {0}", code.NAME); } } } #endregion } } |