Advertisement
Guest User

Untitled

a guest
Jul 4th, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Configuration;
  6. using System.Data.SqlClient;
  7. using System.Text;
  8.  
  9. namespace JTSSQLSERVER
  10. {
  11. class CommonDataManager
  12. {
  13. SqlConnection _conn;
  14. SqlCommand _cmd;
  15. string _commandText;
  16. SqlDataAdapter _da;
  17. DataTable _dt;
  18. List<string> _commandTexts;
  19.  
  20. public CommonDataManager()
  21. {
  22. Initialize(true);
  23. }
  24.  
  25. public CommonDataManager(string query)
  26. {
  27. this._commandText = query;
  28. Initialize();
  29. }
  30.  
  31. public int ParameterCount
  32. {
  33. get { return _cmd.Parameters.Count; }
  34. }
  35.  
  36. public void AddWithValue(string name, object param)
  37. {
  38. if (_cmd != null)
  39. {
  40. _cmd.Parameters.AddWithValue(name, param);
  41. }
  42. }
  43.  
  44. public void SetNullParameterValue(string name, SqlDbType tipe)
  45. {
  46. _cmd.Parameters[name].Value = DBNull.Value;
  47. }
  48.  
  49. public void AddNullParameter(string name, SqlDbType tipe, int lenght)
  50. {
  51. _cmd.Parameters.Add(name, tipe, lenght);
  52. }
  53.  
  54. public void ClearParameters()
  55. {
  56. if (_cmd.Parameters.Count > 0)
  57. {
  58. _cmd.Parameters.Clear();
  59. }
  60. }
  61.  
  62. public DataTable GetDataTable()
  63. {
  64. _da.Fill(_dt);
  65. return _dt;
  66. }
  67.  
  68. public int ExecuteNonQuery()
  69. {
  70. _conn.Open();
  71. int result = _cmd.ExecuteNonQuery();
  72. _conn.Close();
  73. return result;
  74. }
  75.  
  76. public object ExecuteScalar()
  77. {
  78. _conn.Open();
  79. object result = _cmd.ExecuteScalar();
  80. _conn.Close();
  81. return result;
  82. }
  83.  
  84. public void AddTransactionQuery(string transaction)
  85. {
  86. if (this._commandTexts != null)
  87. {
  88. this._commandTexts.Add(transaction);
  89. }
  90. }
  91.  
  92. public void ClearTransactionQueries()
  93. {
  94. if (this._commandTexts.Count > 0)
  95. {
  96. this._commandTexts.Clear();
  97. }
  98. }
  99.  
  100.  
  101. public void Initialize()
  102. {
  103. this._conn = new SqlConnection(ConfigurationManager.ConnectionStrings["JTS"].ConnectionString);
  104. if (!string.IsNullOrEmpty(this._commandText))
  105. {
  106. this._cmd = new SqlCommand(this._commandText, this._conn);
  107. this._da = new SqlDataAdapter();
  108. this._dt = new DataTable();
  109. _da.SelectCommand = _cmd;
  110. this._commandTexts = new List<string>();
  111. }
  112. }
  113.  
  114. public void Initialize(bool transaction)
  115. {
  116. if (transaction)
  117. {
  118. this._conn = new SqlConnection(ConfigurationManager.ConnectionStrings["JTS"].ConnectionString);
  119.  
  120. this._cmd = new SqlCommand(this._commandText, this._conn);
  121. this._da = new SqlDataAdapter();
  122. this._dt = new DataTable();
  123. _da.SelectCommand = _cmd;
  124. this._commandTexts = new List<string>();
  125. }
  126. }
  127.  
  128. public bool ExecuteTransaction()
  129. {
  130. using (_conn)
  131. {
  132. _conn.Open();
  133. SqlTransaction transaction = _conn.BeginTransaction();
  134. _cmd.Transaction = transaction;
  135.  
  136. try
  137. {
  138. foreach (string query in _commandTexts)
  139. {
  140. _cmd.CommandText = query;
  141. _cmd.ExecuteNonQuery();
  142. }
  143. transaction.Commit();
  144. return true;
  145. }
  146. catch (Exception ex)
  147. {
  148. try
  149. {
  150. transaction.Rollback();
  151. }
  152. catch (Exception ex2)
  153. {
  154.  
  155. }
  156. }
  157. }
  158. return false;
  159. }
  160.  
  161. public void SetCommandText(string query)
  162. {
  163. this._commandText = query;
  164. _cmd.CommandText = this._commandText;
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement