纽威
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.

299 lines
11 KiB

3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace NFine.Data.Extensions
  10. {
  11. public class DbFactory
  12. {
  13. /// <summary>
  14. /// 根据配置文件中所配置的数据库类型
  15. /// 来获取命令参数中的参数符号oracle为":",sqlserver为"@"
  16. /// </summary>
  17. /// <returns></returns>
  18. public static string CreateDbParmCharacter()
  19. {
  20. string character = string.Empty;
  21. switch (DbHelper.DbType)
  22. {
  23. case DatabaseType.SqlServer:
  24. character = "@";
  25. break;
  26. case DatabaseType.Oracle:
  27. character = ":";
  28. break;
  29. case DatabaseType.MySql:
  30. character = "?";
  31. break;
  32. case DatabaseType.Access:
  33. character = "@";
  34. break;
  35. case DatabaseType.SQLite:
  36. character = "@";
  37. break;
  38. default:
  39. throw new Exception("数据库类型目前不支持!");
  40. }
  41. return character;
  42. }
  43. /// <summary>
  44. /// 根据配置文件中所配置的数据库类型和传入的
  45. /// 数据库链接字符串来创建相应数据库连接对象
  46. /// </summary>
  47. /// <param name="connectionString"></param>
  48. /// <returns></returns>
  49. public static DbConnection CreateDbConnection(string connectionString)
  50. {
  51. DbConnection conn = null;
  52. switch (DbHelper.DbType)
  53. {
  54. case DatabaseType.SqlServer:
  55. conn = new SqlConnection(connectionString);
  56. break;
  57. //case DatabaseType.Oracle:
  58. // conn = new OracleConnection(connectionString);
  59. // break;
  60. //case DatabaseType.MySql:
  61. // conn = new MySqlConnection(connectionString);
  62. // break;
  63. //case DatabaseType.Access:
  64. // conn = new OleDbConnection(connectionString);
  65. // break;
  66. //case DatabaseType.SQLite:
  67. // conn = new SQLiteConnection(connectionString);
  68. // break;
  69. default:
  70. throw new Exception("数据库类型目前不支持!");
  71. }
  72. return conn;
  73. }
  74. /// <summary>
  75. /// 根据配置文件中所配置的数据库类型
  76. /// 来创建相应数据库命令对象
  77. /// </summary>
  78. /// <returns></returns>
  79. public static DbCommand CreateDbCommand()
  80. {
  81. DbCommand cmd = null;
  82. switch (DbHelper.DbType)
  83. {
  84. case DatabaseType.SqlServer:
  85. cmd = new SqlCommand();
  86. break;
  87. //case DatabaseType.Oracle:
  88. // cmd = new OracleCommand();
  89. // break;
  90. //case DatabaseType.MySql:
  91. // cmd = new MySqlCommand();
  92. // break;
  93. //case DatabaseType.Access:
  94. // cmd = new OleDbCommand();
  95. // break;
  96. //case DatabaseType.SQLite:
  97. // cmd = new SQLiteCommand();
  98. // break;
  99. default:
  100. throw new Exception("数据库类型目前不支持!");
  101. }
  102. return cmd;
  103. }
  104. /// <summary>
  105. /// 根据配置文件中所配置的数据库类型
  106. /// 来创建相应数据库适配器对象
  107. /// </summary>
  108. /// <returns></returns>
  109. public static IDbDataAdapter CreateDataAdapter()
  110. {
  111. IDbDataAdapter adapter = null;
  112. switch (DbHelper.DbType)
  113. {
  114. case DatabaseType.SqlServer:
  115. adapter = new SqlDataAdapter();
  116. break;
  117. //case DatabaseType.Oracle:
  118. // adapter = new OracleDataAdapter();
  119. // break;
  120. //case DatabaseType.MySql:
  121. // adapter = new MySqlDataAdapter();
  122. // break;
  123. //case DatabaseType.Access:
  124. // adapter = new OleDbDataAdapter();
  125. // break;
  126. //case DatabaseType.SQLite:
  127. // adapter = new SQLiteDataAdapter();
  128. // break;
  129. default:
  130. throw new Exception("数据库类型目前不支持!");
  131. }
  132. return adapter;
  133. }
  134. /// <summary>
  135. /// 根据配置文件中所配置的数据库类型
  136. /// 和传入的命令对象来创建相应数据库适配器对象
  137. /// </summary>
  138. /// <returns></returns>
  139. public static IDbDataAdapter CreateDataAdapter(DbCommand cmd)
  140. {
  141. IDbDataAdapter adapter = null;
  142. switch (DbHelper.DbType)
  143. {
  144. case DatabaseType.SqlServer:
  145. adapter = new SqlDataAdapter((SqlCommand)cmd);
  146. break;
  147. //case DatabaseType.Oracle:
  148. // adapter = new OracleDataAdapter((OracleCommand)cmd);
  149. // break;
  150. //case DatabaseType.MySql:
  151. // adapter = new MySqlDataAdapter((MySqlCommand)cmd);
  152. // break;
  153. //case DatabaseType.Access:
  154. // adapter = new OleDbDataAdapter((OleDbCommand)cmd);
  155. // break;
  156. //case DatabaseType.SQLite:
  157. // adapter = new SQLiteDataAdapter((SQLiteCommand)cmd);
  158. // break;
  159. default: throw new Exception("数据库类型目前不支持!");
  160. }
  161. return adapter;
  162. }
  163. /// <summary>
  164. /// 根据配置文件中所配置的数据库类型
  165. /// 来创建相应数据库的参数对象
  166. /// </summary>
  167. /// <returns></returns>
  168. public static DbParameter CreateDbParameter()
  169. {
  170. DbParameter param = null;
  171. switch (DbHelper.DbType)
  172. {
  173. case DatabaseType.SqlServer:
  174. param = new SqlParameter();
  175. break;
  176. //case DatabaseType.Oracle:
  177. // param = new OracleParameter();
  178. // break;
  179. //case DatabaseType.MySql:
  180. // param = new MySqlParameter();
  181. // break;
  182. //case DatabaseType.Access:
  183. // param = new OleDbParameter();
  184. // break;
  185. //case DatabaseType.SQLite:
  186. // param = new SQLiteParameter();
  187. // break;
  188. default:
  189. throw new Exception("数据库类型目前不支持!");
  190. }
  191. return param;
  192. }
  193. /// <summary>
  194. /// 根据配置文件中所配置的数据库类型
  195. /// 来创建相应数据库的参数对象
  196. /// </summary>
  197. /// <returns></returns>
  198. public static DbParameter CreateDbParameter(string paramName, object value)
  199. {
  200. DbParameter param = DbFactory.CreateDbParameter();
  201. param.ParameterName = paramName;
  202. param.Value = value;
  203. return param;
  204. }
  205. /// <summary>
  206. /// 根据配置文件中所配置的数据库类型
  207. /// 来创建相应数据库的参数对象
  208. /// </summary>
  209. /// <returns></returns>
  210. public static DbParameter CreateDbParameter(string paramName, object value, DbType dbType)
  211. {
  212. DbParameter param = DbFactory.CreateDbParameter();
  213. param.DbType = dbType;
  214. param.ParameterName = paramName;
  215. param.Value = value;
  216. return param;
  217. }
  218. /// <summary>
  219. /// 根据配置文件中所配置的数据库类型
  220. /// 来创建相应数据库的参数对象
  221. /// </summary>
  222. /// <returns></returns>
  223. public static DbParameter CreateDbParameter(string paramName, object value, DbType dbType, int size)
  224. {
  225. DbParameter param = DbFactory.CreateDbParameter();
  226. param.DbType = dbType;
  227. param.ParameterName = paramName;
  228. param.Value = value;
  229. param.Size = size;
  230. return param;
  231. }
  232. /// <summary>
  233. /// 根据配置文件中所配置的数据库类型
  234. /// 来创建相应数据库的参数对象
  235. /// </summary>
  236. /// <returns></returns>
  237. public static DbParameter CreateDbParameter(string paramName, object value, int size)
  238. {
  239. DbParameter param = DbFactory.CreateDbParameter();
  240. param.ParameterName = paramName;
  241. param.Value = value;
  242. param.Size = size;
  243. return param;
  244. }
  245. /// <summary>
  246. /// 根据配置文件中所配置的数据库类型
  247. /// 来创建相应数据库的参数对象
  248. /// </summary>
  249. /// <returns></returns>
  250. public static DbParameter CreateDbOutParameter(string paramName, int size)
  251. {
  252. DbParameter param = DbFactory.CreateDbParameter();
  253. param.Direction = ParameterDirection.Output;
  254. param.ParameterName = paramName;
  255. param.Size = size;
  256. return param;
  257. }
  258. /// <summary>
  259. /// 根据配置文件中所配置的数据库类型
  260. /// 和传入的参数来创建相应数据库的sql语句对应参数对象
  261. /// </summary>
  262. /// <returns></returns>
  263. public static DbParameter[] CreateDbParameters(int size)
  264. {
  265. int i = 0;
  266. DbParameter[] param = null;
  267. switch (DbHelper.DbType)
  268. {
  269. case DatabaseType.SqlServer:
  270. param = new SqlParameter[size];
  271. while (i < size) { param[i] = new SqlParameter(); i++; }
  272. break;
  273. //case DatabaseType.Oracle:
  274. // param = new OracleParameter[size];
  275. // while (i < size) { param[i] = new OracleParameter(); i++; }
  276. // break;
  277. //case DatabaseType.MySql:
  278. // param = new MySqlParameter[size];
  279. // while (i < size) { param[i] = new MySqlParameter(); i++; }
  280. // break;
  281. //case DatabaseType.Access:
  282. // param = new OleDbParameter[size];
  283. // while (i < size) { param[i] = new OleDbParameter(); i++; }
  284. // break;
  285. //case DatabaseType.SQLite:
  286. // param = new SQLiteParameter[size];
  287. // while (i < size) { param[i] = new SQLiteParameter(); i++; }
  288. // break;
  289. default:
  290. throw new Exception("数据库类型目前不支持!");
  291. }
  292. return param;
  293. }
  294. }
  295. }