AccessHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.OleDb;
  6. namespace ErpServer
  7. {
  8. public class AccessHelper
  9. {
  10. public AccessHelper()
  11. {
  12. }
  13. /// <summary>
  14. /// 执行sql语句
  15. /// </summary>
  16. /// <param name="sqlstr"></param>
  17. public static int excuteSql(string connStr, string sqlstr)
  18. {
  19. using (OleDbConnection conn = new OleDbConnection(connStr))
  20. {
  21. try
  22. {
  23. conn.Open();
  24. OleDbCommand comm = new OleDbCommand();
  25. comm.Connection = conn;
  26. comm.CommandType = CommandType.Text;
  27. comm.CommandText = sqlstr;
  28. return comm.ExecuteNonQuery();
  29. }
  30. catch (Exception e)
  31. {
  32. return 0;
  33. throw new Exception(connStr + "|" + e.Message);
  34. }
  35. finally
  36. {
  37. conn.Close();
  38. conn.Dispose();
  39. }
  40. }
  41. }
  42. public static void ExecuteSqlTran(string connStr, ArrayList SQLStringList)
  43. {
  44. using (OleDbConnection conn = new OleDbConnection(connStr))
  45. {
  46. conn.Open();
  47. OleDbCommand cmd = new OleDbCommand();
  48. cmd.Connection = conn;
  49. OleDbTransaction tx = conn.BeginTransaction();
  50. cmd.Transaction = tx;
  51. try
  52. {
  53. for (int n = 0; n < SQLStringList.Count; n++)
  54. {
  55. string strsql = SQLStringList[n].ToString();
  56. if (strsql.Trim().Length > 1)
  57. {
  58. cmd.CommandText = strsql;
  59. cmd.ExecuteNonQuery();
  60. }
  61. }
  62. tx.Commit();
  63. }
  64. catch (System.Data.OleDb.OleDbException E)
  65. {
  66. tx.Rollback();
  67. throw new Exception(E.Message);
  68. }
  69. }
  70. }
  71. public static void ExecuteMultiQuery(string conn, List<string> lst)
  72. {
  73. using (OleDbConnection inconn = new OleDbConnection(conn))
  74. {
  75. inconn.Open();
  76. OleDbTransaction myTrans = inconn.BeginTransaction();
  77. OleDbCommand incmd = inconn.CreateCommand();
  78. incmd.Transaction = myTrans;
  79. foreach (string sql in lst)
  80. {
  81. incmd.CommandText = sql;
  82. incmd.ExecuteNonQuery();
  83. }
  84. myTrans.Commit();
  85. }
  86. }
  87. public static object excuteScalar(string connStr, string sqlstr)
  88. {
  89. using (OleDbConnection conn = new OleDbConnection(connStr))
  90. {
  91. try
  92. {
  93. conn.Open();
  94. OleDbCommand comm = new OleDbCommand();
  95. comm.Connection = conn;
  96. comm.CommandType = CommandType.Text;
  97. comm.CommandText = sqlstr;
  98. object val = comm.ExecuteScalar();
  99. return val;
  100. }
  101. catch (Exception e)
  102. {
  103. conn.Close();
  104. conn.Dispose();
  105. return null;
  106. throw new Exception(connStr + "|" + e.Message);
  107. }
  108. finally
  109. {
  110. conn.Close();
  111. conn.Dispose();
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
  117. /// </summary>
  118. /// <param name="sqlstr"></param>
  119. /// <returns></returns>
  120. public static OleDbDataReader dataReader(string connStr, string sqlstr)
  121. {
  122. OleDbDataReader dr = null;
  123. using (OleDbConnection conn = new OleDbConnection(connStr))
  124. {
  125. try
  126. {
  127. conn.Open();
  128. OleDbCommand comm = new OleDbCommand();
  129. comm.Connection = conn;
  130. comm.CommandText = sqlstr;
  131. comm.CommandType = CommandType.Text;
  132. dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
  133. return dr;
  134. }
  135. catch
  136. {
  137. dr.Close();
  138. return null;
  139. }
  140. finally
  141. {
  142. conn.Close();
  143. conn.Dispose();
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 返回指定sql语句的datatable
  149. /// </summary>
  150. /// <param name="sqlstr"></param>
  151. /// <returns></returns>
  152. public static DataTable dataTable(string connStr, string sqlstr)
  153. {
  154. DataTable dt = new DataTable();
  155. using (OleDbConnection conn = new OleDbConnection(connStr))
  156. {
  157. try
  158. {
  159. conn.Open();
  160. OleDbCommand comm = new OleDbCommand();
  161. comm.Connection = conn;
  162. comm.CommandType = CommandType.Text;
  163. comm.CommandText = sqlstr;
  164. OleDbDataAdapter da = new OleDbDataAdapter(comm);
  165. da.Fill(dt);
  166. }
  167. catch (Exception e)
  168. {
  169. conn.Close();
  170. conn.Dispose();
  171. return null;
  172. throw new Exception(sqlstr + "|" + e.Message);
  173. }
  174. finally
  175. {
  176. conn.Close();
  177. conn.Dispose();
  178. }
  179. return dt;
  180. }
  181. }
  182. #region ExecuteDataset
  183. public static DataSet ExecuteDataset(string connectionString, string commandText)
  184. {
  185. return ExecuteDataset(connectionString, commandText, (OleDbParameter[])null);
  186. }
  187. public static DataSet ExecuteDataset(string connectionString, string commandText, params OleDbParameter[] commandParameters)
  188. {
  189. if (null == connectionString && string.Empty == connectionString) throw new ArgumentNullException("connectionString");
  190. using (OleDbConnection connection = new OleDbConnection(connectionString))
  191. {
  192. return ExecuteDataset(connection, commandText, commandParameters);
  193. }
  194. }
  195. public static DataSet ExecuteDataset(OleDbConnection connection, string commandText)
  196. {
  197. return ExecuteDataset(connection, commandText, (OleDbParameter[])null);
  198. }
  199. public static DataSet ExecuteDataset(OleDbConnection connection, string commandText, params OleDbParameter[] commandParameters)
  200. {
  201. if (null == connection) throw new ArgumentNullException("connection");
  202. try
  203. {
  204. OleDbCommand cmd = new OleDbCommand();
  205. bool mustCloseConnection = false;
  206. PrepareCommand(cmd, connection, (OleDbTransaction)null, commandText, commandParameters, out mustCloseConnection);
  207. using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
  208. {
  209. DataSet ds = new DataSet();
  210. da.Fill(ds);
  211. cmd.Parameters.Clear();
  212. if (mustCloseConnection)
  213. connection.Close();
  214. return ds;
  215. }
  216. }
  217. catch
  218. {
  219. return null;
  220. }
  221. finally
  222. {
  223. if (connection != null)
  224. {
  225. connection.Close();
  226. connection.Dispose();
  227. }
  228. }
  229. }
  230. public static DataSet ExecuteDataset(OleDbTransaction transaction, string commandText)
  231. {
  232. return ExecuteDataset(transaction, commandText, (OleDbParameter[])null);
  233. }
  234. public static DataSet ExecuteDataset(OleDbTransaction transaction, string commandText, params OleDbParameter[] commandParameters)
  235. {
  236. if (null == transaction) throw new ArgumentNullException("transaction");
  237. if (null != transaction && null == transaction.Connection) throw new ArgumentException("transaction对象commited和rollbacked需要一个已打开的事务", "transaction");
  238. OleDbCommand cmd = new OleDbCommand();
  239. bool mustCloseConnection = false;
  240. PrepareCommand(cmd, transaction.Connection, transaction, commandText, commandParameters, out mustCloseConnection);
  241. using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
  242. {
  243. DataSet ds = new DataSet();
  244. da.Fill(ds);
  245. cmd.Parameters.Clear();
  246. return ds;
  247. }
  248. }
  249. #endregion ExecuteDataset
  250. private static void PrepareCommand(OleDbCommand command, OleDbConnection connection, OleDbTransaction transaction, string commandText, OleDbParameter[] commandParameters, out bool mustCloseConnection)
  251. {
  252. if (null == command) throw new ArgumentNullException("command");
  253. if (null == commandText || string.Empty == commandText) throw new ArgumentNullException("commandText");
  254. if (ConnectionState.Closed == connection.State)
  255. {
  256. connection.Open();
  257. mustCloseConnection = true;
  258. }
  259. else
  260. {
  261. mustCloseConnection = false;
  262. }
  263. command.Connection = connection;
  264. command.CommandText = commandText;
  265. if (null != transaction)
  266. {
  267. if (null == transaction.Connection) throw new ArgumentException("transaction对象commited和rollbacked需要一个已打开的事务", "transaction");
  268. command.Transaction = transaction;
  269. }
  270. command.CommandType = CommandType.Text;
  271. if (null != commandParameters)
  272. {
  273. AttachParameters(command, commandParameters);
  274. }
  275. }
  276. private static void AttachParameters(OleDbCommand command, OleDbParameter[] commandParameters)
  277. {
  278. if (null == command) throw new ArgumentNullException("command");
  279. if (null != commandParameters)
  280. {
  281. foreach (OleDbParameter p in commandParameters)
  282. {
  283. if (null != p)
  284. {
  285. if ((ParameterDirection.Input == p.Direction || ParameterDirection.InputOutput == p.Direction) && (null == p.Value))
  286. {
  287. p.Value = DBNull.Value;
  288. }
  289. command.Parameters.Add(p);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }