DataBase.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. namespace SIMDP.DAL.DalData
  9. {
  10. public class DataBase
  11. {
  12. private SqlConnection Con()
  13. {
  14. return new SqlConnection(ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString);
  15. }
  16. public int ExecuteSql(string sql)
  17. {
  18. SqlConnection con = this.Con();
  19. SqlCommand com = new SqlCommand(sql, con);
  20. int result;
  21. try
  22. {
  23. con.Open();
  24. com.ExecuteNonQuery();
  25. result = 1;
  26. }
  27. catch (SqlException e)
  28. {
  29. throw new Exception(e.Message);
  30. }
  31. finally
  32. {
  33. com.Dispose();
  34. con.Close();
  35. }
  36. return result;
  37. }
  38. public int ExecuteSql(string sql, SqlParameter[] p)
  39. {
  40. SqlConnection con = this.Con();
  41. SqlCommand com = new SqlCommand(sql, con);
  42. for (int i = 0; i < p.Length; i++)
  43. {
  44. com.Parameters.Add(p[i]);
  45. }
  46. int result;
  47. try
  48. {
  49. con.Open();
  50. com.ExecuteNonQuery();
  51. result = 1;
  52. }
  53. catch (SqlException e)
  54. {
  55. throw new Exception(e.Message);
  56. }
  57. finally
  58. {
  59. com.Parameters.Clear();
  60. com.Dispose();
  61. con.Close();
  62. }
  63. return result;
  64. }
  65. public int ExecuteSqls(string[] sql)
  66. {
  67. SqlConnection con = this.Con();
  68. SqlCommand com = new SqlCommand();
  69. int i = sql.Length;
  70. con.Open();
  71. SqlTransaction tran = con.BeginTransaction();
  72. int result;
  73. try
  74. {
  75. com.Connection = con;
  76. com.Transaction = tran;
  77. for (int j = 0; j < sql.Length; j++)
  78. {
  79. string str = sql[j];
  80. com.CommandText = str;
  81. com.ExecuteNonQuery();
  82. }
  83. tran.Commit();
  84. result = 1;
  85. }
  86. catch (SqlException e)
  87. {
  88. tran.Rollback();
  89. throw new Exception(e.Message);
  90. }
  91. finally
  92. {
  93. com.Dispose();
  94. con.Close();
  95. }
  96. return result;
  97. }
  98. public int ExecuteSqls(string[] sql, SqlParameter[] p)
  99. {
  100. SqlConnection con = this.Con();
  101. SqlCommand com = new SqlCommand();
  102. int i = sql.Length;
  103. int j = p.Length;
  104. for (int k = 0; k < j; k++)
  105. {
  106. com.Parameters.Add(p[k]);
  107. }
  108. con.Open();
  109. SqlTransaction tran = con.BeginTransaction();
  110. int result;
  111. try
  112. {
  113. com.Connection = con;
  114. com.Transaction = tran;
  115. for (int l = 0; l < sql.Length; l++)
  116. {
  117. string str = sql[l];
  118. com.CommandText = str;
  119. com.ExecuteNonQuery();
  120. }
  121. tran.Commit();
  122. result = 1;
  123. }
  124. catch (SqlException e)
  125. {
  126. tran.Rollback();
  127. throw new Exception(e.Message);
  128. }
  129. finally
  130. {
  131. com.Parameters.Clear();
  132. com.Dispose();
  133. con.Close();
  134. }
  135. return result;
  136. }
  137. public string ExecuteValue(string sql)
  138. {
  139. SqlConnection con = this.Con();
  140. SqlCommand com = new SqlCommand(sql, con);
  141. string result;
  142. try
  143. {
  144. con.Open();
  145. object ob = com.ExecuteScalar();
  146. if (ob != null)
  147. {
  148. result = ob.ToString();
  149. }
  150. else
  151. {
  152. result = null;
  153. }
  154. }
  155. catch (SqlException e)
  156. {
  157. throw new Exception(e.Message);
  158. }
  159. finally
  160. {
  161. com.Dispose();
  162. con.Dispose();
  163. }
  164. return result;
  165. }
  166. public string ExecuteValue(string sql, SqlParameter[] p)
  167. {
  168. SqlConnection con = this.Con();
  169. SqlCommand com = new SqlCommand(sql, con);
  170. for (int i = 0; i < p.Length; i++)
  171. {
  172. com.Parameters.Add(p[i]);
  173. }
  174. string result;
  175. try
  176. {
  177. con.Open();
  178. object ob = com.ExecuteScalar();
  179. if (ob != null)
  180. {
  181. result = ob.ToString();
  182. }
  183. else
  184. {
  185. result = null;
  186. }
  187. }
  188. catch (SqlException e)
  189. {
  190. throw new Exception(e.Message);
  191. }
  192. finally
  193. {
  194. com.Parameters.Clear();
  195. com.Dispose();
  196. con.Close();
  197. }
  198. return result;
  199. }
  200. public DataTable GetDataTable(string sql)
  201. {
  202. DataTable dt = new DataTable();
  203. SqlDataAdapter da = new SqlDataAdapter(sql, this.Con());
  204. da.Fill(dt);
  205. return dt;
  206. }
  207. public DataSet GetDataSet(string sql,string DataTableName)
  208. {
  209. DataSet ds = new DataSet();
  210. SqlDataAdapter da = new SqlDataAdapter(sql, this.Con());
  211. da.Fill(ds, DataTableName);
  212. return ds;
  213. }
  214. public DataTable GetDataTable(string sql, SqlParameter[] param)
  215. {
  216. DataTable dt = new DataTable();
  217. SqlConnection conn = this.Con();
  218. SqlCommand cmd = new SqlCommand(sql, conn);
  219. for (int i = 0; i < param.Length; i++)
  220. {
  221. cmd.Parameters.Add(param[i]);
  222. }
  223. SqlDataAdapter da = new SqlDataAdapter(cmd);
  224. da.Fill(dt);
  225. return dt;
  226. }
  227. public DataTable GetDataTableproc(string proc, SqlParameter[] param)
  228. {
  229. DataTable dt = new DataTable();
  230. SqlConnection conn = this.Con();
  231. SqlCommand cmd = new SqlCommand(proc, conn);
  232. cmd.CommandType = CommandType.StoredProcedure;
  233. for (int i = 0; i < param.Length; i++)
  234. {
  235. cmd.Parameters.Add(param[i]);
  236. }
  237. SqlDataAdapter da = new SqlDataAdapter(cmd);
  238. da.Fill(dt);
  239. return dt;
  240. }
  241. public string ExecuteValueProc(string sql, SqlParameter[] p)
  242. {
  243. SqlConnection con = this.Con();
  244. SqlCommand com = new SqlCommand(sql, con);
  245. com.CommandType = CommandType.StoredProcedure;
  246. for (int i = 0; i < p.Length; i++)
  247. {
  248. com.Parameters.Add(p[i]);
  249. }
  250. string result;
  251. try
  252. {
  253. con.Open();
  254. object ob = com.ExecuteScalar();
  255. if (ob != null)
  256. {
  257. result = ob.ToString();
  258. }
  259. else
  260. {
  261. result = null;
  262. }
  263. }
  264. catch (SqlException e)
  265. {
  266. throw new Exception(e.Message);
  267. }
  268. finally
  269. {
  270. com.Parameters.Clear();
  271. com.Dispose();
  272. con.Close();
  273. }
  274. return result;
  275. }
  276. public SqlDataReader getdr(string sql)
  277. {
  278. SqlConnection con = Con();
  279. con.Open();
  280. SqlCommand com = new SqlCommand(sql, con);
  281. SqlDataReader dr = com.ExecuteReader();
  282. return dr;
  283. }
  284. }
  285. }