| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.OleDb;
- namespace ErpServer
- {
- public class AccessHelper
- {
- public AccessHelper()
- {
- }
- /// <summary>
- /// 执行sql语句
- /// </summary>
- /// <param name="sqlstr"></param>
- public static int excuteSql(string connStr, string sqlstr)
- {
- using (OleDbConnection conn = new OleDbConnection(connStr))
- {
- try
- {
- conn.Open();
- OleDbCommand comm = new OleDbCommand();
- comm.Connection = conn;
- comm.CommandType = CommandType.Text;
- comm.CommandText = sqlstr;
- return comm.ExecuteNonQuery();
- }
- catch (Exception e)
- {
- return 0;
- throw new Exception(connStr + "|" + e.Message);
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- }
- }
- public static void ExecuteSqlTran(string connStr, ArrayList SQLStringList)
- {
- using (OleDbConnection conn = new OleDbConnection(connStr))
- {
- conn.Open();
- OleDbCommand cmd = new OleDbCommand();
- cmd.Connection = conn;
- OleDbTransaction tx = conn.BeginTransaction();
- cmd.Transaction = tx;
- try
- {
- for (int n = 0; n < SQLStringList.Count; n++)
- {
- string strsql = SQLStringList[n].ToString();
- if (strsql.Trim().Length > 1)
- {
- cmd.CommandText = strsql;
- cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- }
- catch (System.Data.OleDb.OleDbException E)
- {
- tx.Rollback();
- throw new Exception(E.Message);
- }
- }
- }
- public static void ExecuteMultiQuery(string conn, List<string> lst)
- {
- using (OleDbConnection inconn = new OleDbConnection(conn))
- {
- inconn.Open();
- OleDbTransaction myTrans = inconn.BeginTransaction();
- OleDbCommand incmd = inconn.CreateCommand();
- incmd.Transaction = myTrans;
- foreach (string sql in lst)
- {
- incmd.CommandText = sql;
- incmd.ExecuteNonQuery();
- }
- myTrans.Commit();
- }
- }
- public static object excuteScalar(string connStr, string sqlstr)
- {
- using (OleDbConnection conn = new OleDbConnection(connStr))
- {
- try
- {
- conn.Open();
- OleDbCommand comm = new OleDbCommand();
- comm.Connection = conn;
- comm.CommandType = CommandType.Text;
- comm.CommandText = sqlstr;
- object val = comm.ExecuteScalar();
- return val;
- }
- catch (Exception e)
- {
- conn.Close();
- conn.Dispose();
- return null;
- throw new Exception(connStr + "|" + e.Message);
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- }
- }
- /// <summary>
- /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
- /// </summary>
- /// <param name="sqlstr"></param>
- /// <returns></returns>
- public static OleDbDataReader dataReader(string connStr, string sqlstr)
- {
- OleDbDataReader dr = null;
- using (OleDbConnection conn = new OleDbConnection(connStr))
- {
- try
- {
- conn.Open();
- OleDbCommand comm = new OleDbCommand();
- comm.Connection = conn;
- comm.CommandText = sqlstr;
- comm.CommandType = CommandType.Text;
- dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
- return dr;
- }
- catch
- {
- dr.Close();
- return null;
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- }
- }
- /// <summary>
- /// 返回指定sql语句的datatable
- /// </summary>
- /// <param name="sqlstr"></param>
- /// <returns></returns>
- public static DataTable dataTable(string connStr, string sqlstr)
- {
- DataTable dt = new DataTable();
- using (OleDbConnection conn = new OleDbConnection(connStr))
- {
- try
- {
- conn.Open();
- OleDbCommand comm = new OleDbCommand();
- comm.Connection = conn;
- comm.CommandType = CommandType.Text;
- comm.CommandText = sqlstr;
- OleDbDataAdapter da = new OleDbDataAdapter(comm);
- da.Fill(dt);
- }
- catch (Exception e)
- {
- conn.Close();
- conn.Dispose();
- return null;
- throw new Exception(sqlstr + "|" + e.Message);
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- return dt;
- }
- }
- #region ExecuteDataset
- public static DataSet ExecuteDataset(string connectionString, string commandText)
- {
- return ExecuteDataset(connectionString, commandText, (OleDbParameter[])null);
- }
- public static DataSet ExecuteDataset(string connectionString, string commandText, params OleDbParameter[] commandParameters)
- {
- if (null == connectionString && string.Empty == connectionString) throw new ArgumentNullException("connectionString");
- using (OleDbConnection connection = new OleDbConnection(connectionString))
- {
- return ExecuteDataset(connection, commandText, commandParameters);
- }
- }
- public static DataSet ExecuteDataset(OleDbConnection connection, string commandText)
- {
- return ExecuteDataset(connection, commandText, (OleDbParameter[])null);
- }
- public static DataSet ExecuteDataset(OleDbConnection connection, string commandText, params OleDbParameter[] commandParameters)
- {
- if (null == connection) throw new ArgumentNullException("connection");
- try
- {
- OleDbCommand cmd = new OleDbCommand();
- bool mustCloseConnection = false;
- PrepareCommand(cmd, connection, (OleDbTransaction)null, commandText, commandParameters, out mustCloseConnection);
- using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- da.Fill(ds);
- cmd.Parameters.Clear();
- if (mustCloseConnection)
- connection.Close();
- return ds;
- }
- }
- catch
- {
- return null;
- }
- finally
- {
- if (connection != null)
- {
- connection.Close();
- connection.Dispose();
- }
- }
- }
- public static DataSet ExecuteDataset(OleDbTransaction transaction, string commandText)
- {
- return ExecuteDataset(transaction, commandText, (OleDbParameter[])null);
- }
- public static DataSet ExecuteDataset(OleDbTransaction transaction, string commandText, params OleDbParameter[] commandParameters)
- {
- if (null == transaction) throw new ArgumentNullException("transaction");
- if (null != transaction && null == transaction.Connection) throw new ArgumentException("transaction对象commited和rollbacked需要一个已打开的事务", "transaction");
- OleDbCommand cmd = new OleDbCommand();
- bool mustCloseConnection = false;
- PrepareCommand(cmd, transaction.Connection, transaction, commandText, commandParameters, out mustCloseConnection);
- using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- da.Fill(ds);
- cmd.Parameters.Clear();
- return ds;
- }
- }
- #endregion ExecuteDataset
- private static void PrepareCommand(OleDbCommand command, OleDbConnection connection, OleDbTransaction transaction, string commandText, OleDbParameter[] commandParameters, out bool mustCloseConnection)
- {
- if (null == command) throw new ArgumentNullException("command");
- if (null == commandText || string.Empty == commandText) throw new ArgumentNullException("commandText");
- if (ConnectionState.Closed == connection.State)
- {
- connection.Open();
- mustCloseConnection = true;
- }
- else
- {
- mustCloseConnection = false;
- }
- command.Connection = connection;
- command.CommandText = commandText;
- if (null != transaction)
- {
- if (null == transaction.Connection) throw new ArgumentException("transaction对象commited和rollbacked需要一个已打开的事务", "transaction");
- command.Transaction = transaction;
- }
- command.CommandType = CommandType.Text;
- if (null != commandParameters)
- {
- AttachParameters(command, commandParameters);
- }
- }
- private static void AttachParameters(OleDbCommand command, OleDbParameter[] commandParameters)
- {
- if (null == command) throw new ArgumentNullException("command");
- if (null != commandParameters)
- {
- foreach (OleDbParameter p in commandParameters)
- {
- if (null != p)
- {
- if ((ParameterDirection.Input == p.Direction || ParameterDirection.InputOutput == p.Direction) && (null == p.Value))
- {
- p.Value = DBNull.Value;
- }
- command.Parameters.Add(p);
- }
- }
- }
- }
- }
- }
|