自己寫的數據庫工具類

做了類似SqlHelper的工作

 public class SqlAccess : IDisposable
    {
        [ThreadStatic]
        private static int _level;
        private bool isCommit = true;
        private SqlConnection _connection;
        private SqlCommand _command;
        private SqlDataAdapter _dataAdapter;
        private SqlBulkCopy _bulkCopy;

        public SqlAccess(string connectionString)
        {
            ConnectionString = connectionString;
        }

        public string ConnectionString { get; set; }

        protected SqlConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    _connection = new SqlConnection();
                    _connection.ConnectionString = ConnectionString;

                    if (_connection.State == ConnectionState.Closed)
                    {
                        _connection.Open();
                    }
                }
                return _connection;
            }
        }

        protected SqlCommand Command
        {
            get
            {
                if (_command == null)
                {
                    _command = Connection.CreateCommand();
                }
                return _command;
            }
        }

        protected SqlDataAdapter DataAdapter
        {
            get
            {
                if (_dataAdapter == null)
                {
                    _dataAdapter = new SqlDataAdapter();
                    _dataAdapter.SelectCommand = Command;
                }
                return _dataAdapter;
            }
        }

        protected SqlBulkCopy BulkCopy
        {
            get
            {
                if (_bulkCopy == null)
                {
                    _bulkCopy = new SqlBulkCopy(Connection, SqlBulkCopyOptions.CheckConstraints, Command.Transaction);
                }
                return _bulkCopy;
            }
        }

        public SqlParameterCollection Parameters
        {
            get
            {
                return Command.Parameters;
            }
        }

        public SqlBulkCopyColumnMappingCollection BulkCopyColumnMapping
        {
            get
            {
                return BulkCopy.ColumnMappings;
            }
        }

        public void BeginTransaction()
        {
            if (_level == 0)
            {
                if (Command.Transaction == null)
                {
                    Command.Transaction = Connection.BeginTransaction();
                }
            }
            _level++;
            isCommit = false;
        }

        public void Commit()
        {
            _level--;
            if (_level == 0)
            {
                Command.Transaction.Commit();
            }
            isCommit = true;
        }

        public void Rollback()
        {
            Command.Transaction.Rollback();
            isCommit = true;
        }

        public SqlAccess SetCommandText(string commandText)
        {
            Command.CommandType = CommandType.Text;
            Command.CommandText = commandText;
            return this;
        }

        public SqlAccess SetCommandProcedure(string commandText)
        {
            Command.CommandType = CommandType.StoredProcedure;
            Command.CommandText = commandText;
            return this;
        }

        public SqlAccess SetCommandTable(string commandText)
        {
            Command.CommandType = CommandType.TableDirect;
            Command.CommandText = commandText;
            return this;
        }

        public SqlAccess SetBulkCopy(string tableName)
        {
            BulkCopy.DestinationTableName = tableName;
            return this;
        }


        public int ExecuteNonQuery()
        {
            int index = Command.ExecuteNonQuery();
            Command.Parameters.Clear();
            return index;
        }

        public object ExecuteScalar()
        {
            object result = Command.ExecuteScalar();
            Command.Parameters.Clear();
            return result;
        }

        public DataSet GetDataSet()
        {
            DataSet dataSet = new DataSet();
            DataAdapter.Fill(dataSet);
            Command.Parameters.Clear();
            return dataSet;
        }

        public DataTable GetDataTable()
        {
            DataTable dataTable = new DataTable();
            DataAdapter.Fill(dataTable);
            Command.Parameters.Clear();
            return dataTable;
        }

        public void WriteToServer(DataTable table)
        {
            BulkCopy.WriteToServer(table);
            BulkCopy.ColumnMappings.Clear();
        }


        public void Dispose()
        {
            try
            {
                if (!isCommit)
                {
                    Rollback();
                }
                if (_bulkCopy != null)
                {
                    _bulkCopy.Close();
                    _bulkCopy = null;
                }
                if (_dataAdapter != null)
                {
                    _dataAdapter.Dispose();
                    _dataAdapter = null;
                }
                if (_command != null)
                {
                    _command.Cancel();
                    _command.Dispose();
                    _command = null;
                }
                if (_connection != null)
                {
                    _connection.Close();
                    _connection.Dispose();
                    _connection = null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("數據處理異常", ex);
            }
            finally
            {
                _dataAdapter = null;
                _command = null;
                _connection = null;
            }

            GC.SuppressFinalize(this);
        }
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章