You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
13 KiB

2 years ago
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.OleDb;
  6. using System.Data.OracleClient;
  7. using System.Data.SqlClient;
  8. namespace EasyNet.DBUtility
  9. {
  10. public class DbFactory
  11. {
  12. /// <summary>
  13. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �������R�O�ѼƤ����ѼƲŸ�oracle��":",sqlserver��"@"
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string CreateDbParmCharacter()
  17. {
  18. var character = string.Empty;
  19. switch (AdoHelper.DbType)
  20. {
  21. case DatabaseType.SQLSERVER:
  22. character = "@";
  23. break;
  24. case DatabaseType.ORACLE:
  25. character = ":";
  26. break;
  27. case DatabaseType.MYSQL:
  28. character = "?";
  29. break;
  30. case DatabaseType.ACCESS:
  31. character = "@";
  32. break;
  33. default:
  34. throw new Exception("�ƾڮw�����ثe�������I");
  35. }
  36. return character;
  37. }
  38. /// <summary>
  39. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw�����M�ǤJ�� ���Ʈw�s���r���ӳЫج������Ʈw�s�u����
  40. /// </summary>
  41. /// <param name="connectionString"></param>
  42. /// <returns></returns>
  43. public static IDbConnection CreateDbConnection(string connectionString)
  44. {
  45. IDbConnection conn = null;
  46. switch (AdoHelper.DbType)
  47. {
  48. case DatabaseType.SQLSERVER:
  49. conn = new SqlConnection(connectionString);
  50. break;
  51. case DatabaseType.MYSQL:
  52. conn = new MySqlConnection(connectionString);
  53. break;
  54. case DatabaseType.ACCESS:
  55. conn = new OleDbConnection(connectionString);
  56. break;
  57. default:
  58. throw new Exception("�ƾڮw�����ثe�������I");
  59. }
  60. return conn;
  61. }
  62. /// <summary>
  63. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  64. /// </summary>
  65. /// <returns></returns>
  66. public static IDbCommand CreateDbCommand()
  67. {
  68. IDbCommand cmd = null;
  69. switch (AdoHelper.DbType)
  70. {
  71. case DatabaseType.SQLSERVER:
  72. cmd = new SqlCommand();
  73. break;
  74. case DatabaseType.MYSQL:
  75. cmd = new MySqlCommand();
  76. break;
  77. case DatabaseType.ACCESS:
  78. cmd = new OleDbCommand();
  79. break;
  80. default:
  81. throw new Exception("�ƾڮw�����ثe�������I");
  82. }
  83. return cmd;
  84. }
  85. /// <summary>
  86. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�A�t������
  87. /// </summary>
  88. /// <returns></returns>
  89. public static IDbDataAdapter CreateDataAdapter()
  90. {
  91. IDbDataAdapter adapter = null;
  92. switch (AdoHelper.DbType)
  93. {
  94. case DatabaseType.SQLSERVER:
  95. adapter = new SqlDataAdapter();
  96. break;
  97. case DatabaseType.MYSQL:
  98. adapter = new MySqlDataAdapter();
  99. break;
  100. case DatabaseType.ACCESS:
  101. adapter = new OleDbDataAdapter();
  102. break;
  103. default:
  104. throw new Exception("�ƾڮw�����ثe�������I");
  105. }
  106. return adapter;
  107. }
  108. /// <summary>
  109. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �M�ǤJ���R�O�����ӳЫج������Ʈw�A�t������
  110. /// </summary>
  111. /// <param name="cmd">todo: describe cmd parameter on CreateDataAdapter</param>
  112. /// <returns></returns>
  113. public static IDbDataAdapter CreateDataAdapter(IDbCommand cmd)
  114. {
  115. IDbDataAdapter adapter = null;
  116. switch (AdoHelper.DbType)
  117. {
  118. case DatabaseType.SQLSERVER:
  119. adapter = new SqlDataAdapter((SqlCommand)cmd);
  120. break;
  121. case DatabaseType.MYSQL:
  122. adapter = new MySqlDataAdapter((MySqlCommand)cmd);
  123. break;
  124. case DatabaseType.ACCESS:
  125. adapter = new OleDbDataAdapter((OleDbCommand)cmd);
  126. break;
  127. default: throw new Exception("�ƾڮw�����ثe�������I");
  128. }
  129. return adapter;
  130. }
  131. /// <summary>
  132. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw���Ѽƪ���
  133. /// </summary>
  134. /// <returns></returns>
  135. public static IDbDataParameter CreateDbParameter()
  136. {
  137. IDbDataParameter param = null;
  138. switch (AdoHelper.DbType)
  139. {
  140. case DatabaseType.SQLSERVER:
  141. param = new SqlParameter();
  142. break;
  143. case DatabaseType.ORACLE:
  144. param = new OracleParameter();
  145. break;
  146. case DatabaseType.MYSQL:
  147. param = new MySqlParameter();
  148. break;
  149. case DatabaseType.ACCESS:
  150. param = new OleDbParameter();
  151. break;
  152. default:
  153. throw new Exception("�ƾڮw�����ثe�������I");
  154. }
  155. return param;
  156. }
  157. /// <summary>
  158. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  159. /// </summary>
  160. /// <param name="paramName">todo: describe paramName parameter on CreateDbParameter</param>
  161. /// <param name="value">todo: describe value parameter on CreateDbParameter</param>
  162. /// <returns></returns>
  163. public static IDbDataParameter CreateDbParameter(string paramName, object value)
  164. {
  165. if (AdoHelper.DbType == DatabaseType.ACCESS)
  166. {
  167. paramName = "@" + paramName;
  168. }
  169. var param = DbFactory.CreateDbParameter();
  170. param.ParameterName = paramName;
  171. param.Value = value;
  172. return param;
  173. }
  174. /// <summary>
  175. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  176. /// </summary>
  177. /// <param name="paramName">todo: describe paramName parameter on CreateDbParameter</param>
  178. /// <param name="value">todo: describe value parameter on CreateDbParameter</param>
  179. /// <param name="dbType">todo: describe dbType parameter on CreateDbParameter</param>
  180. /// <returns></returns>
  181. public static IDbDataParameter CreateDbParameter(string paramName, object value, DbType dbType)
  182. {
  183. if (AdoHelper.DbType == DatabaseType.ACCESS)
  184. {
  185. paramName = "@" + paramName;
  186. }
  187. var param = DbFactory.CreateDbParameter();
  188. param.DbType = dbType;
  189. param.ParameterName = paramName;
  190. param.Value = value;
  191. return param;
  192. }
  193. /// <summary>
  194. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  195. /// </summary>
  196. /// <param name="paramName">todo: describe paramName parameter on CreateDbParameter</param>
  197. /// <param name="value">todo: describe value parameter on CreateDbParameter</param>
  198. /// <param name="direction">todo: describe direction parameter on CreateDbParameter</param>
  199. /// <returns></returns>
  200. public static IDbDataParameter CreateDbParameter(string paramName, object value, ParameterDirection direction)
  201. {
  202. if (AdoHelper.DbType == DatabaseType.ACCESS)
  203. {
  204. paramName = "@" + paramName;
  205. }
  206. var param = DbFactory.CreateDbParameter();
  207. param.Direction = direction;
  208. param.ParameterName = paramName;
  209. param.Value = value;
  210. return param;
  211. }
  212. /// <summary>
  213. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  214. /// </summary>
  215. /// <param name="paramName">todo: describe paramName parameter on CreateDbParameter</param>
  216. /// <param name="value">todo: describe value parameter on CreateDbParameter</param>
  217. /// <param name="size">todo: describe size parameter on CreateDbParameter</param>
  218. /// <param name="direction">todo: describe direction parameter on CreateDbParameter</param>
  219. /// <returns></returns>
  220. public static IDbDataParameter CreateDbParameter(string paramName, object value, int size, ParameterDirection direction)
  221. {
  222. if (AdoHelper.DbType == DatabaseType.ACCESS)
  223. {
  224. paramName = "@" + paramName;
  225. }
  226. var param = DbFactory.CreateDbParameter();
  227. param.Direction = direction;
  228. param.ParameterName = paramName;
  229. param.Value = value;
  230. param.Size = size;
  231. return param;
  232. }
  233. /// <summary>
  234. /// CreateDbParameters
  235. /// </summary>
  236. /// <param name="paramName">todo: describe paramName parameter on CreateDbOutParameter</param>
  237. /// <param name="size">todo: describe size parameter on CreateDbOutParameter</param>
  238. /// <returns></returns>
  239. public static IDbDataParameter CreateDbOutParameter(string paramName, int size)
  240. {
  241. if (AdoHelper.DbType == DatabaseType.ACCESS)
  242. {
  243. paramName = "@" + paramName;
  244. }
  245. var param = DbFactory.CreateDbParameter();
  246. param.Direction = ParameterDirection.Output;
  247. param.ParameterName = paramName;
  248. param.Size = size;
  249. return param;
  250. }
  251. /// <summary>
  252. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  253. /// </summary>
  254. /// <param name="paramName">todo: describe paramName parameter on CreateDbParameter</param>
  255. /// <param name="value">todo: describe value parameter on CreateDbParameter</param>
  256. /// <param name="dbType">todo: describe dbType parameter on CreateDbParameter</param>
  257. /// <param name="direction">todo: describe direction parameter on CreateDbParameter</param>
  258. /// <returns></returns>
  259. public static IDbDataParameter CreateDbParameter(string paramName, object value, DbType dbType, ParameterDirection direction)
  260. {
  261. if (AdoHelper.DbType == DatabaseType.ACCESS)
  262. {
  263. paramName = "@" + paramName;
  264. }
  265. var param = DbFactory.CreateDbParameter();
  266. param.Direction = direction;
  267. param.DbType = dbType;
  268. param.ParameterName = paramName;
  269. param.Value = value;
  270. return param;
  271. }
  272. /// <summary>
  273. /// �ھڳ]�w�ɤ��Ұt�m�����Ʈw���� �ӳЫج������Ʈw�R�O����
  274. /// </summary>
  275. /// <param name="size">todo: describe size parameter on CreateDbParameters</param>
  276. /// <returns></returns>
  277. public static IDbDataParameter[] CreateDbParameters(int size)
  278. {
  279. var i = 0;
  280. IDbDataParameter[] param = null;
  281. switch (AdoHelper.DbType)
  282. {
  283. case DatabaseType.SQLSERVER:
  284. param = new SqlParameter[size];
  285. while (i < size) { param[i] = new SqlParameter(); i++; }
  286. break;
  287. case DatabaseType.ORACLE:
  288. param = new OracleParameter[size];
  289. while (i < size) { param[i] = new OracleParameter(); i++; }
  290. break;
  291. case DatabaseType.MYSQL:
  292. param = new MySqlParameter[size];
  293. while (i < size) { param[i] = new MySqlParameter(); i++; }
  294. break;
  295. case DatabaseType.ACCESS:
  296. param = new OleDbParameter[size];
  297. while (i < size) { param[i] = new OleDbParameter(); i++; }
  298. break;
  299. default:
  300. throw new Exception("�ƾڮw�����ثe�������I");
  301. }
  302. return param;
  303. }
  304. /// <summary>
  305. /// �X��Dictionary
  306. /// </summary>
  307. /// <param name="dic1">todo: describe dic1 parameter on ApllyDic</param>
  308. /// <param name="dic2">todo: describe dic2 parameter on ApllyDic</param>
  309. /// <returns></returns>
  310. public static Dictionary<string, object> ApllyDic(Dictionary<string, object> dic1, Dictionary<string, object> dic2)
  311. {
  312. var newDic = new Dictionary<string, object>();
  313. foreach (string key in dic1.Keys)
  314. {
  315. if (!newDic.ContainsKey(key))
  316. {
  317. newDic.Add(key, dic1[key]);
  318. }
  319. }
  320. foreach (string key in dic2.Keys)
  321. {
  322. if (!newDic.ContainsKey(key))
  323. {
  324. newDic.Add(key, dic2[key]);
  325. }
  326. }
  327. return newDic;
  328. }
  329. /// <summary>
  330. /// </summary>
  331. /// <returns></returns>
  332. public static IDbTransaction CreateDbTransaction()
  333. {
  334. var conn = CreateDbConnection(AdoHelper.ConnectionString);
  335. if (conn.State == ConnectionState.Closed)
  336. {
  337. conn.Open();
  338. }
  339. return conn.BeginTransaction();
  340. }
  341. }
  342. }