PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/QuickMon4/QuickMonDBAgents/Generic/GenericSQLServerDAL.cs

#
C# | 324 lines | 315 code | 9 blank | 0 comment | 25 complexity | 4d69e2a6b6a0c9aaa0a13f3111512a41 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. namespace HenIT.Data.SqlClient
  9. {
  10. public delegate void RaiseDBAccessDelegate();
  11. public delegate void RaiseDBInsertsDelegate(int inserts);
  12. public delegate void DBAccessDurationDelegate(long milliseconds);
  13. public delegate void RaiseMessageDelegate(string message);
  14. public class GenericSQLServerDAL
  15. {
  16. #region Events
  17. public event RaiseDBAccessDelegate RaiseDBAccess;
  18. protected void DoRaiseDBAccessEvent()
  19. {
  20. if (RaiseDBAccess != null)
  21. {
  22. RaiseDBAccess();
  23. }
  24. }
  25. public event RaiseDBAccessDelegate RaiseDBAccessErrors;
  26. protected void DoRaiseDBAccessErrorsEvent()
  27. {
  28. if (RaiseDBAccessErrors != null)
  29. {
  30. RaiseDBAccessErrors();
  31. }
  32. }
  33. public event RaiseDBInsertsDelegate RaiseDBAccessInserts;
  34. protected void DoRaiseDBAccessInsertsEvent(int inserts)
  35. {
  36. if (RaiseDBAccessInserts != null)
  37. {
  38. RaiseDBAccessInserts(inserts);
  39. }
  40. }
  41. public event RaiseDBAccessDelegate RaiseDBAccessInsertDuplicates;
  42. protected void DoRaiseDBAccessInsertDuplicates()
  43. {
  44. if (RaiseDBAccessInsertDuplicates != null)
  45. {
  46. RaiseDBAccessInsertDuplicates();
  47. }
  48. }
  49. public event DBAccessDurationDelegate DBAccessDurationInfoMessage;
  50. protected void RaiseDBAccessDurationInfoMessage(long milliseconds)
  51. {
  52. if (DBAccessDurationInfoMessage != null)
  53. {
  54. DBAccessDurationInfoMessage(milliseconds);
  55. }
  56. }
  57. public event RaiseMessageDelegate RaiseInfoMessage;
  58. protected void DoRaiseInfoMessage(string message)
  59. {
  60. if (RaiseInfoMessage != null)
  61. {
  62. RaiseInfoMessage(message);
  63. }
  64. }
  65. public event RaiseMessageDelegate RaiseErrorMessage;
  66. protected void DoRaiseErrorMessage(string message)
  67. {
  68. if (RaiseErrorMessage != null)
  69. {
  70. RaiseErrorMessage(message);
  71. }
  72. }
  73. public event RaiseMessageDelegate RaiseSyncInfoMessage;
  74. protected void DoRaiseSyncInfoMessage(string message)
  75. {
  76. if (RaiseSyncInfoMessage != null)
  77. {
  78. RaiseSyncInfoMessage(message);
  79. }
  80. }
  81. #endregion
  82. #region Connection details
  83. #region Connection properties
  84. private string server = string.Empty;
  85. public string Server
  86. {
  87. get { return server; }
  88. set { server = value; }
  89. }
  90. private string database = string.Empty;
  91. public string Database
  92. {
  93. get { return database; }
  94. set { database = value; }
  95. }
  96. private string userName = string.Empty;
  97. public string UserName
  98. {
  99. get { return userName; }
  100. set { userName = value; }
  101. }
  102. private string password = string.Empty;
  103. public string Password
  104. {
  105. protected get { return password; }
  106. set { password = value; }
  107. }
  108. private int commandTimeout = 120;
  109. public int CommandTimeout
  110. {
  111. get { return commandTimeout; }
  112. set { commandTimeout = value; }
  113. }
  114. #endregion
  115. protected string lastError = "";
  116. public string LastError
  117. {
  118. get { return lastError; }
  119. }
  120. protected string connectionString;
  121. public string ConnectionString
  122. {
  123. get { return connectionString; }
  124. set { connectionString = value; }
  125. }
  126. public void SetConnection()
  127. {
  128. if (server.Length > 0 && database.Length > 0)
  129. {
  130. SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder();
  131. sqlbuilder.DataSource = server;
  132. sqlbuilder.InitialCatalog = database;
  133. if (userName.Length > 0)
  134. {
  135. sqlbuilder.UserID = userName;
  136. sqlbuilder.Password = password;
  137. sqlbuilder.IntegratedSecurity = false;
  138. }
  139. else
  140. sqlbuilder.IntegratedSecurity = true;
  141. connectionString = sqlbuilder.ConnectionString;
  142. }
  143. else
  144. {
  145. throw new Exception("Connection settings not set properly!");
  146. }
  147. }
  148. public void SetConnection(string sqlServer, string database)
  149. {
  150. server = sqlServer;
  151. this.database = database;
  152. SetConnection();
  153. }
  154. public void SetConnection(string sqlServer, string database, string userName, string password)
  155. {
  156. server = sqlServer;
  157. this.database = database;
  158. this.userName = userName;
  159. this.password = password;
  160. SetConnection();
  161. }
  162. #endregion
  163. #region GetSingleValue
  164. public object GetSingleValue(string sql)
  165. {
  166. return GetSingleValue(sql, CommandType.StoredProcedure, new SqlParameter[] { });
  167. }
  168. public object GetSingleValue(string sql, CommandType commandType)
  169. {
  170. return GetSingleValue(sql, commandType, new SqlParameter[] { });
  171. }
  172. public object GetSingleValue(string sql, CommandType commandType, SqlParameter[] parms)
  173. {
  174. object returnValue = null;
  175. Stopwatch sw = new Stopwatch();
  176. sw.Start();
  177. DoRaiseDBAccessEvent();
  178. try
  179. {
  180. using (SqlConnection conn = new SqlConnection(connectionString))
  181. {
  182. conn.Open();
  183. using (SqlCommand cmnd = new SqlCommand(sql, conn))
  184. {
  185. cmnd.CommandTimeout = commandTimeout;
  186. cmnd.CommandType = commandType;
  187. if (parms != null)
  188. cmnd.Parameters.AddRange(parms);
  189. returnValue = cmnd.ExecuteScalar();
  190. }
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. lastError = ex.Message;
  196. DoRaiseDBAccessErrorsEvent();
  197. throw;
  198. }
  199. sw.Stop();
  200. RaiseDBAccessDurationInfoMessage(sw.ElapsedMilliseconds);
  201. return returnValue;
  202. }
  203. #endregion
  204. #region GetDataSet
  205. public DataSet GetDataSet(string sql)
  206. {
  207. return GetDataSet(sql, CommandType.StoredProcedure, null);
  208. }
  209. public DataSet GetDataSet(string sql, CommandType commandType)
  210. {
  211. return GetDataSet(sql, commandType, null);
  212. }
  213. public DataSet GetDataSet(string sql, CommandType commandType, SqlParameter[] parms)
  214. {
  215. DataSet returnDS = new DataSet();
  216. Stopwatch sw = new Stopwatch();
  217. sw.Start();
  218. DoRaiseDBAccessEvent();
  219. try
  220. {
  221. using (SqlConnection conn = new SqlConnection(connectionString))
  222. {
  223. conn.Open();
  224. using (SqlCommand cmnd = new SqlCommand(sql, conn))
  225. {
  226. cmnd.CommandTimeout = commandTimeout;
  227. cmnd.CommandType = commandType;
  228. if (parms != null)
  229. cmnd.Parameters.AddRange(parms);
  230. using (SqlDataAdapter da = new SqlDataAdapter(cmnd))
  231. {
  232. da.Fill(returnDS);
  233. }
  234. }
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. lastError = ex.Message;
  240. DoRaiseDBAccessErrorsEvent();
  241. throw;
  242. }
  243. sw.Stop();
  244. RaiseDBAccessDurationInfoMessage(sw.ElapsedMilliseconds);
  245. return returnDS;
  246. }
  247. #endregion
  248. #region Execute
  249. public void ExecuteSP(string spName)
  250. {
  251. ExecuteSP(spName, null, commandTimeout);
  252. }
  253. public void ExecuteSP(string spName, SqlParameter[] parms)
  254. {
  255. ExecuteSP(spName, parms, commandTimeout);
  256. }
  257. public void ExecuteSP(string sql, SqlParameter[] parms, int commandTimeout)
  258. {
  259. Stopwatch sw = new Stopwatch();
  260. sw.Start();
  261. DoRaiseDBAccessEvent();
  262. try
  263. {
  264. using (SqlConnection conn = new SqlConnection(connectionString))
  265. {
  266. conn.Open();
  267. using (SqlCommand cmnd = new SqlCommand(sql, conn))
  268. {
  269. cmnd.CommandType = CommandType.StoredProcedure;
  270. cmnd.CommandTimeout = commandTimeout;
  271. if (parms != null)
  272. cmnd.Parameters.AddRange(parms);
  273. cmnd.ExecuteNonQuery();
  274. }
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. lastError = ex.Message;
  280. DoRaiseDBAccessErrorsEvent();
  281. throw;
  282. }
  283. sw.Stop();
  284. RaiseDBAccessDurationInfoMessage(sw.ElapsedMilliseconds);
  285. }
  286. public void ExecuteSQL(string sql)
  287. {
  288. Stopwatch sw = new Stopwatch();
  289. sw.Start();
  290. DoRaiseDBAccessEvent();
  291. try
  292. {
  293. using (SqlConnection conn = new SqlConnection(connectionString))
  294. {
  295. conn.Open();
  296. using (SqlCommand cmnd = new SqlCommand(sql, conn))
  297. {
  298. cmnd.CommandType = CommandType.Text;
  299. cmnd.CommandTimeout = commandTimeout;
  300. cmnd.ExecuteNonQuery();
  301. }
  302. }
  303. }
  304. catch (Exception ex)
  305. {
  306. lastError = ex.Message;
  307. DoRaiseDBAccessErrorsEvent();
  308. throw;
  309. }
  310. sw.Stop();
  311. RaiseDBAccessDurationInfoMessage(sw.ElapsedMilliseconds);
  312. }
  313. #endregion
  314. }
  315. }