Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Created September 26, 2019 00:38
Show Gist options
  • Save IngIeoAndSpare/d7c977456b0de30ca8196bfc19c0998c to your computer and use it in GitHub Desktop.
Save IngIeoAndSpare/d7c977456b0de30ca8196bfc19c0998c to your computer and use it in GitHub Desktop.
Db connect sample gist
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageFileAnalysis.DTO;
namespace ImageFileAnalysis.Modules.DbModuel
{
class DbConnectHandler
{
public PostgresqlDB postgresql;
private string connectionInfo = "";
private int dbFlag = 0;
private string m_postgresConnect = "Host={0};Port={1};Username={2};password={3};database={4}";
private string m_mysqlConnect = "server={0}:{1};user={2};password={3}database={4}";
workResultDTO result = new workResultDTO();
public DbConnectHandler(string propertiesFilePath, int dbFlag)
{
this.dbFlag = dbFlag;
PropertiesReader dbInfoReader = new PropertiesReader(propertiesFilePath);
//XXX: 아래부분은 using 사용 불가능?
workResultDTO result = dbInfoReader.readDbConnectionInfo();
if (result.workResult)
{
connectionInfo = getDbConnectionInfo(dbInfoReader.dbInfo);
} else
{
//TOOD : err handler
}
}
public workResultDTO dbConnection()
{
workResultDTO dbResult = new workResultDTO();
if(dbFlag == 0)
{
//postgres db
postgresql = new PostgresqlDB(connectionInfo);
dbResult = postgresql.dbConnectionOepn();
} else if (dbFlag == 1)
{
//mysql db
}
return dbResult;
}
private string getDbConnectionInfo(DbConnectionInfoDTO dbInfo)
{
string dbConnectionFrame = "";
//선택된 db종류에 따라 connection 내용을 바꾼다.
switch (this.dbFlag)
{
case 0:
dbConnectionFrame = m_postgresConnect;
break;
case 1:
dbConnectionFrame = m_mysqlConnect;
break;
}
string dbConnection = string.Format(
dbConnectionFrame,
dbInfo.dbHost,
dbInfo.dbPort,
dbInfo.dbUserName,
dbInfo.dbUserPassword,
dbInfo.dbName
);
return dbConnection;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageFileAnalysis.DTO
{
class DbConnectionInfoDTO
{
public string dbHost { get; set; }
public string dbPort { get; set; }
public string dbUserName { get; set; }
public string dbUserPassword { get; set; }
public string dbName { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using ImageFileAnalysis.DTO;
namespace ImageFileAnalysis.Modules
{
class PropertiesReader
{
private string m_filePath;
public DbConnectionInfoDTO dbInfo = new DbConnectionInfoDTO();
public PropertiesReader(string filePath)
{
this.m_filePath = filePath;
}
/// <summary>
/// db와 관련된 properties파일을 읽고 해당 정보를 객체에 저장하는 메소드
/// 각 properties의 아이템 이름은 dbInfo. 로 시작된다.
/// </summary>
/// <returns>
/// </returns>
public workResultDTO readDbConnectionInfo()
{
workResultDTO result = new workResultDTO();
var data = new Dictionary<string, string>();
try
{
foreach (var rowText in File.ReadAllLines(m_filePath))
{
data.Add(rowText.Split('=')[0].Trim(), string.Join("=", rowText.Split('=').Skip(1).ToArray()).Trim());
}
dbInfo.dbHost = data["dbInfo.dbHost"];
dbInfo.dbName = data["dbInfo.dbName"];
dbInfo.dbUserPassword = data["dbInfo.dbUserPassword"];
dbInfo.dbPort = data["dbInfo.dbPort"];
dbInfo.dbUserName = data["dbInfo.dbUserName"];
result.workResult = true;
} catch (IOException e)
{
result.workResult = false;
result.result = e.ToString();
}
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageFileAnalysis.DTO
{
class workResultDTO
{
/// <summary>
/// true - 작업 성공 || false - 작업 실패
/// </summary>
public bool workResult { get; set; }
/// <summary>
/// 작업 결과물이 들어간다.
/// </summary>
public object result { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment