DataBase Coonection Class


/ Published in: C#
Save to your folder(s)

Simple Class Connection for use in C# Code.

Methods:

- ReturnDataTable(string strQueryCommand)

- ReturnDataSet(string strQueryCommand)

- ExecuteQuery(string strQueryCommand)

- ReturnDataReader(string strQueryCommand)


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. using System.Data;
  8. namespace Database
  9. {
  10. public class DataSettings
  11. {
  12. private SqlConnection Conn;
  13. private SqlCommand Cmd { get; set; }
  14. private SqlDataReader Reader;
  15. private SqlDataAdapter dataAdapter;
  16. private DataSet Ds { get; set; }
  17.  
  18. public DataSettings()
  19. {
  20. Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"]);
  21. }
  22.  
  23. public SqlConnection OpenConn()
  24. {
  25. if (Conn.State == System.Data.ConnectionState.Closed || Conn.State == System.Data.ConnectionState.Broken)
  26. {
  27. Conn.Open();
  28. }
  29. return Conn;
  30. }
  31.  
  32. public SqlConnection CloseConn()
  33. {
  34. if (Conn.State == System.Data.ConnectionState.Open)
  35. {
  36. Conn.Close();
  37. }
  38.  
  39. return Conn;
  40. }
  41.  
  42.  
  43. public DataTable ReturnDataTable(string strQueryCommand)
  44. {
  45. Cmd = new SqlCommand();
  46. SqlDataAdapter adap;
  47. try
  48. {
  49. Cmd.Connection = OpenConn();
  50. Cmd.CommandText = strQueryCommand;
  51. adap = new SqlDataAdapter(Cmd);
  52. DataTable dt = new DataTable();
  53. adap.Fill(dt);
  54.  
  55. return dt;
  56. }
  57. catch (Exception Ex)
  58. {
  59. throw Ex;
  60. }
  61. finally
  62. {
  63. Cmd = null;
  64. }
  65. }
  66.  
  67. public DataSet ReturnDataSet(string strQueryCommand)
  68. {
  69. try
  70. {
  71. OpenConn();
  72. Cmd = Conn.CreateCommand();
  73. dataAdapter = new SqlDataAdapter(strQueryCommand, Conn);
  74. Ds = new DataSet();
  75. Ds.Reset();
  76. dataAdapter.Fill(Ds);
  77.  
  78. return Ds;
  79. }
  80. catch (Exception ex)
  81. {
  82.  
  83. throw ex;
  84. }
  85. finally
  86. {
  87. Cmd = null;
  88. CloseConn();
  89. }
  90. }
  91.  
  92. public void ExecuteQuery(string strQueryCommand)
  93. {
  94. try
  95. {
  96. Cmd.Connection = OpenConn();
  97. Cmd.CommandText = strQueryCommand;
  98. Cmd.ExecuteNonQuery();
  99. }
  100. catch (Exception ex)
  101. {
  102.  
  103. throw ex;
  104. }
  105. finally
  106. {
  107. Cmd = null;
  108. }
  109.  
  110. }
  111. /// <summary>
  112. /// call datareader
  113. /// </summary>
  114. /// <param name="strQueryCommand"></param>
  115. /// <returns></returns>
  116. public SqlDataReader ReturnDataReader(string strQueryCommand)
  117. {
  118. try
  119. {
  120. Cmd = Conn.CreateCommand();
  121. Cmd.CommandText = strQueryCommand;
  122. Reader = Cmd.ExecuteReader();
  123. return Reader;
  124.  
  125. }
  126. catch (Exception ex)
  127. {
  128. throw ex;
  129. }
  130. finally
  131. {
  132. Cmd = null;
  133. }
  134. }
  135.  
  136.  
  137.  
  138.  
  139. }
  140. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.