自己写的数据库工具类
2015-09-18 09:05
387 查看
做了类似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); } }
相关文章推荐
- [心得记录] MongoDB+nodejs(mac 环境)
- Mysql基础知识点汇总
- 数据库的增删
- mac上简单使用redis
- 数据库学习相关常用语句
- 使用MySQL命令行备份及恢复数据库
- andriod之从Sqlite中获取数据的三种方法SimpleAdapter,SimpCursorAdapter,CursorAdapter
- mysql复习笔记
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- 对于Oracle中分页排序查询语句执行效率的比较分析
- mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database
- 土地利用数据库地图自动缩编软件--地图缩编
- Mysql基础知识点汇总
- 不用安装oracle连接远程oracle数据库
- mysql一个连续日期(天)次数的统计
- mysql数据迁移到mongoDB中,如何操作?
- sql语句基础
- mysql体系结构之不得不说
- 数据库事务的学习笔记
- HSQLDB 安装与使用