C#에 있는 DataTable은 너무 많은 정보를 들고 있어 무겁기에 비슷하지만 더 가벼운 형식으로 구현.
MySqlTable Class
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 88 89 90 91 92 93 94 95 96 97 98 99 100 | public class MySqlTable<T> { public List<string> Columns { get; set; } public string AddColumn { set { if (Columns.Contains(value) == true) new ArgumentException("이미 등록된 컬럼값 입니다."); Columns.Add(value); } } public List<List<T>> Rows { get; set; } public Int32 Count { get { return Rows.Count; } } public T this[int index] { get { return Rows[0][index]; } } public T this[int row, int index] { get { return Rows[row][index]; } } public T this[int row, string name] { get { Int32 index = Columns.FindIndex(t => t == name); if (index == -1) new ArgumentException("해당 Column이 존재하지 않습니다."); return Rows[row][index]; } } public MySqlTable() { Columns = new List<string>(); Rows = new List<List<T>>(); } public void CreateTable(IDataReader reader) { try { Columns = new List<string>(reader.FieldCount); for (int i = 0; i < reader.FieldCount; i++) { AddColumn = reader.GetName(i); } while (reader.Read()) { List<T> row = new List<T>(Columns.Count); for (int i = 0; i < Columns.Count; i++) { if (reader.GetValue(i) == DBNull.Value) row.Add(default(T)); else row.Add((T)reader.GetValue(i)); } Rows.Add(row); } } catch (Exception ex) { new DataException("CreateTable : " + ex.Message); //new ArgumentException("해당 Column이 존재하지 않습니다."); } } | cs |
MySqlManager Class
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 | private static readonly Dictionary<string, object> emptyParameter = new Dictionary<string, object>(0); public static MySqlTable<T> ExecuteMySqlTable<T>(string connectionString, string procedureName, out Int32 resultCode) { return ExecuteMySqlTableImpl<T>(connectionString, procedureName, emptyParameter, out resultCode); } public static MySqlTable<T> ExecuteMySqlTable<T>(string connectionString, string procedureName, Dictionary<string, object> paramsList, out Int32 resultCode) { return ExecuteMySqlTableImpl<T>(connectionString, procedureName, paramsList, out resultCode); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static MySqlTable<T> ExecuteMySqlTableImpl<T>(string connectionString, string procedureName, Dictionary<string, object> paramsList, out Int32 resultCode) { MySqlTable<T> mysqlTable = new MySqlTable<T>(); resultCode = (Int32)ErrorNumbers.SQL_UNKNOWN_FAILED; using (MySqlConnection sqlConnection = new MySqlConnection(connectionString)) using (MySqlCommand sqlCommand = new MySqlCommand(procedureName, sqlConnection)) { sqlCommand.CommandTimeout = ServiceConfig.REQUEST_DB_TIMEOUT_SECOND; sqlCommand.CommandType = CommandType.StoredProcedure; foreach (KeyValuePair<string, object> pair in paramsList) { sqlCommand.Parameters.AddWithValue(pair.Key, pair.Value); } try { sqlConnection.Open(); using (IDataReader r = sqlCommand.ExecuteReader()) { if (r.Read()) resultCode = (Int32)r.GetInt32(0); if(r.NextResult() == true) mysqlTable.CreateTable(r); } } catch (Exception ex) { resultCode = (Int32)ErrorNumbers.SQL_EXCEPTION; WebServerLogger.Instance.Write(LogTypes.ERROR, 1, string.Format("Error List<T> ExecuteReader - ProcedureName : {0}, ex : {1}", procedureName, ex.Message)); } } return mysqlTable; } | cs |
사용 예.
1 2 3 4 5 6 7 8 | Dictionary<string, object> paramList = new Dictionary<string, object> { ["$user_key"] = requestPacket.UserKey, ["$uuid"] = requestPacket.UserUUID, }; MySqlTable<object> readerList = MySqlManager.ExecuteMySqlTable<object>(ServiceConfig.db_ConnectionString, MySqlProcedureName.sp_UserStageScore, paramList, out Int32 resultCode); responsePacket.ResultNumber = resultCode; | cs |
'C#' 카테고리의 다른 글
17# C# Protobuf-net에서 List<List<T>> 사용법 (0) | 2019.04.03 |
---|---|
16# C# Protobuf-net에서 Dictionary 사용법 (0) | 2019.04.03 |
15# C#에서 Buffer.BlockCopy와 Array.Copy 속도 차이 (0) | 2018.12.19 |
14# C#에서 Firebase Topic으로 전체 발송하기 (0) | 2018.07.11 |
13# C#에서 미국 광고 Fyber 처리 (0) | 2018.02.22 |