PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/YUI/YUI.Core/Dao/Session/DbHelper.cs

http://yuicore.googlecode.com/
C# | 370 lines | 328 code | 34 blank | 8 comment | 34 complexity | c20cbaa57af4ec23cec624e6b0e8894b MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Collections;
  5. namespace YUI.Core.Dao.Session
  6. {
  7. public delegate string DecryptEventHandler(string connectionString);
  8. public sealed class DbHelper
  9. {
  10. private DecryptEventHandler decryptProxy;
  11. private DbProviderFactory dbFactory;
  12. private string dbConnectionString;
  13. public DbHelper(string connectionString, DbProviderFactory dbFactory)
  14. {
  15. this.dbConnectionString = connectionString;
  16. this.dbFactory = dbFactory;
  17. }
  18. public void SetDecryptHandler(DecryptEventHandler decryptProxy)
  19. {
  20. this.decryptProxy = decryptProxy;
  21. }
  22. #region ??????????
  23. public DbConnection CreateConnection()
  24. {
  25. DbConnection dbconn = dbFactory.CreateConnection();
  26. if (decryptProxy != null)
  27. {
  28. dbConnectionString = decryptProxy(dbConnectionString);
  29. decryptProxy = null;
  30. }
  31. dbconn.ConnectionString = dbConnectionString;
  32. return dbconn;
  33. }
  34. /// <summary>
  35. /// ????
  36. /// </summary>
  37. /// <returns></returns>
  38. public DbCommand CreateCommand()
  39. {
  40. return dbFactory.CreateCommand();
  41. }
  42. /// <summary>
  43. /// ????
  44. /// </summary>
  45. /// <returns></returns>
  46. public DbParameter CreateParameter()
  47. {
  48. return dbFactory.CreateParameter();
  49. }
  50. #endregion
  51. public DbCommand CreateStoredProcCommand(string procName)
  52. {
  53. DbCommand dbCommand = dbFactory.CreateCommand();
  54. dbCommand.CommandType = CommandType.StoredProcedure;
  55. dbCommand.CommandText = procName;
  56. return dbCommand;
  57. }
  58. public DbCommand CreateSqlStringCommand(string cmdText)
  59. {
  60. DbCommand dbCommand = dbFactory.CreateCommand();
  61. dbCommand.CommandType = CommandType.Text;
  62. dbCommand.CommandText = cmdText;
  63. return dbCommand;
  64. }
  65. #region ????
  66. public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
  67. {
  68. foreach (DbParameter dbParameter in dbParameterCollection)
  69. {
  70. cmd.Parameters.Add(dbParameter);
  71. }
  72. }
  73. public void AddInputParameter(DbCommand cmd, string parameterName, DbType dbType, int size, object value)
  74. {
  75. DbParameter dbParameter = cmd.CreateParameter();
  76. dbParameter.DbType = dbType;
  77. dbParameter.ParameterName = parameterName;
  78. dbParameter.Size = size;
  79. dbParameter.Value = value;
  80. dbParameter.Direction = ParameterDirection.Input;
  81. cmd.Parameters.Add(dbParameter);
  82. }
  83. public void AddInputParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
  84. {
  85. DbParameter dbParameter = cmd.CreateParameter();
  86. dbParameter.DbType = dbType;
  87. dbParameter.ParameterName = parameterName;
  88. dbParameter.Value = value;
  89. dbParameter.Direction = ParameterDirection.Input;
  90. cmd.Parameters.Add(dbParameter);
  91. }
  92. public void AddOutputParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
  93. {
  94. DbParameter dbParameter = cmd.CreateParameter();
  95. dbParameter.DbType = dbType;
  96. dbParameter.ParameterName = parameterName;
  97. dbParameter.Size = size;
  98. dbParameter.Direction = ParameterDirection.Output;
  99. cmd.Parameters.Add(dbParameter);
  100. }
  101. public void AddInputOutputParameter(DbCommand cmd, string parameterName, DbType dbType, object value, int size)
  102. {
  103. DbParameter dbParameter = cmd.CreateParameter();
  104. dbParameter.DbType = dbType;
  105. dbParameter.ParameterName = parameterName;
  106. dbParameter.Value = value;
  107. dbParameter.Size = size;
  108. dbParameter.Direction = ParameterDirection.InputOutput;
  109. cmd.Parameters.Add(dbParameter);
  110. }
  111. public void AddReturnValueParameter(DbCommand cmd, string parameterName, DbType dbType)
  112. {
  113. DbParameter dbParameter = cmd.CreateParameter();
  114. dbParameter.DbType = dbType;
  115. dbParameter.ParameterName = parameterName;
  116. dbParameter.Direction = ParameterDirection.ReturnValue;
  117. cmd.Parameters.Add(dbParameter);
  118. }
  119. public DbParameter GetParameter(DbCommand cmd, string parameterName)
  120. {
  121. return cmd.Parameters[parameterName];
  122. }
  123. #endregion
  124. #region ??
  125. public DataSet ExecuteDataSet(DbCommand cmd)
  126. {
  127. cmd.Connection = CreateConnection();
  128. cmd.Connection.Open();
  129. try
  130. {
  131. using (DbDataAdapter dbDataAdapter = dbFactory.CreateDataAdapter())
  132. {
  133. dbDataAdapter.SelectCommand = cmd;
  134. DataSet ds = new DataSet();
  135. dbDataAdapter.Fill(ds);
  136. if (cmd.Connection.State != ConnectionState.Closed)
  137. {
  138. cmd.Connection.Close();
  139. cmd.Connection.Dispose();
  140. }
  141. return ds;
  142. }
  143. }
  144. catch
  145. {
  146. if (cmd.Connection.State != ConnectionState.Closed)
  147. {
  148. cmd.Connection.Close();
  149. cmd.Connection.Dispose();
  150. }
  151. throw;
  152. }
  153. }
  154. public DataTable ExecuteDataTable(DbCommand cmd)
  155. {
  156. cmd.Connection = CreateConnection();
  157. cmd.Connection.Open();
  158. try
  159. {
  160. using (DbDataAdapter dbDataAdapter = dbFactory.CreateDataAdapter())
  161. {
  162. dbDataAdapter.SelectCommand = cmd;
  163. DataTable dataTable = new DataTable();
  164. dbDataAdapter.Fill(dataTable);
  165. if (cmd.Connection.State != ConnectionState.Closed)
  166. {
  167. cmd.Connection.Close();
  168. cmd.Connection.Dispose();
  169. }
  170. return dataTable;
  171. }
  172. }
  173. catch
  174. {
  175. if (cmd.Connection.State != ConnectionState.Closed)
  176. {
  177. cmd.Connection.Close();
  178. cmd.Connection.Dispose();
  179. }
  180. throw;
  181. }
  182. }
  183. public DbDataReader ExecuteReader(DbCommand cmd)
  184. {
  185. cmd.Connection = CreateConnection();
  186. cmd.Connection.Open();
  187. try
  188. {
  189. DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  190. return reader;
  191. }
  192. catch
  193. {
  194. if (cmd.Connection.State != ConnectionState.Closed)
  195. {
  196. cmd.Connection.Close();
  197. cmd.Connection.Dispose();
  198. }
  199. throw;
  200. }
  201. }
  202. public int ExecuteNonQuery(DbCommand cmd)
  203. {
  204. cmd.Connection = CreateConnection();
  205. cmd.Connection.Open();
  206. try
  207. {
  208. int ret = cmd.ExecuteNonQuery();
  209. if (cmd.Connection.State != ConnectionState.Closed)
  210. {
  211. cmd.Connection.Close();
  212. cmd.Connection.Dispose();
  213. }
  214. return ret;
  215. }
  216. catch
  217. {
  218. if (cmd.Connection.State != ConnectionState.Closed)
  219. {
  220. cmd.Connection.Close();
  221. cmd.Connection.Dispose();
  222. }
  223. throw;
  224. }
  225. }
  226. public object ExecuteScalar(DbCommand cmd)
  227. {
  228. cmd.Connection = CreateConnection();
  229. cmd.Connection.Open();
  230. try
  231. {
  232. object ret = cmd.ExecuteScalar();
  233. if (cmd.Connection.State != ConnectionState.Closed)
  234. {
  235. cmd.Connection.Close();
  236. cmd.Connection.Dispose();
  237. }
  238. if (ret == DBNull.Value) ret = null;
  239. return ret;
  240. }
  241. catch
  242. {
  243. if (cmd.Connection.State != ConnectionState.Closed)
  244. {
  245. cmd.Connection.Close();
  246. cmd.Connection.Dispose();
  247. }
  248. throw;
  249. }
  250. }
  251. #endregion
  252. #region ????
  253. public DataSet ExecuteDataSet(DbCommand cmd, DbTrans t)
  254. {
  255. cmd.Connection = t.Connection;
  256. if (t.Transaction != null) cmd.Transaction = t.Transaction;
  257. try
  258. {
  259. using (DbDataAdapter dbDataAdapter = dbFactory.CreateDataAdapter())
  260. {
  261. dbDataAdapter.SelectCommand = cmd;
  262. DataSet ds = new DataSet();
  263. dbDataAdapter.Fill(ds);
  264. return ds;
  265. }
  266. }
  267. catch
  268. {
  269. throw;
  270. }
  271. }
  272. public DataTable ExecuteDataTable(DbCommand cmd, DbTrans t)
  273. {
  274. cmd.Connection = t.Connection;
  275. if (t.Transaction != null) cmd.Transaction = t.Transaction;
  276. try
  277. {
  278. using (DbDataAdapter dbDataAdapter = dbFactory.CreateDataAdapter())
  279. {
  280. dbDataAdapter.SelectCommand = cmd;
  281. DataTable dataTable = new DataTable();
  282. dbDataAdapter.Fill(dataTable);
  283. return dataTable;
  284. }
  285. }
  286. catch
  287. {
  288. throw;
  289. }
  290. }
  291. public DbDataReader ExecuteReader(DbCommand cmd, DbTrans t)
  292. {
  293. cmd.Connection = t.Connection;
  294. if (t.Transaction != null) cmd.Transaction = t.Transaction;
  295. try
  296. {
  297. DbDataReader reader = cmd.ExecuteReader();
  298. return reader;
  299. }
  300. catch
  301. {
  302. throw;
  303. }
  304. }
  305. public int ExecuteNonQuery(DbCommand cmd, DbTrans t)
  306. {
  307. cmd.Connection = t.Connection;
  308. if (t.Transaction != null) cmd.Transaction = t.Transaction;
  309. try
  310. {
  311. int ret = cmd.ExecuteNonQuery();
  312. return ret;
  313. }
  314. catch
  315. {
  316. throw;
  317. }
  318. }
  319. public object ExecuteScalar(DbCommand cmd, DbTrans t)
  320. {
  321. cmd.Connection = t.Connection;
  322. if (t.Transaction != null) cmd.Transaction = t.Transaction;
  323. try
  324. {
  325. object ret = cmd.ExecuteScalar();
  326. if (ret == DBNull.Value) ret = null;
  327. return ret;
  328. }
  329. catch
  330. {
  331. throw;
  332. }
  333. }
  334. #endregion
  335. }
  336. }