usingSystem;usingSystem.Data;namespaceDbManager
{publicsealedclassDBManager : IDBManager, IDisposable
{#region字段privateDataProvider _providerType;privateIDbConnection _idbConnection;privateIDataReader _iDataReader;privateIDbCommand _idbCommand;privateIDbTransaction _idbTransaction;privateIDbDataParameter[] _idbParameters;privatestring_connectionString;#endregion#region构造方法publicDBManager()
{
}publicDBManager(DataProvider providerType)
{
ProviderType=providerType;
}publicDBManager(DataProvider providerType,stringconnectionString)
{
ProviderType=providerType;
ConnectionString=connectionString;
}#endregion#region属性publicDataProvider ProviderType
{get{return_providerType; }set{ _providerType=value; }
}publicIDbConnection Connection
{get{return_idbConnection; }set{ _idbConnection=value; }
}publicIDataReader DataReader
{get{return_iDataReader; }set{ _iDataReader=value; }
}publicIDbCommand Command
{get{return_idbCommand; }set{ _idbCommand=value; }
}publicIDbTransaction Transaction
{get{return_idbTransaction; }set{ _idbTransaction=value; }
}publicIDbDataParameter[] Parameters
{get{return_idbParameters; }set{ _idbParameters=value; }
}publicstringConnectionString
{get{return_connectionString; }set{ _connectionString=value; }
}#endregion#region公有方法publicvoidOpen()
{
Connection=DBManagerFactory.GetConnection(ProviderType);
Connection.ConnectionString=ConnectionString;if(Connection.State!=ConnectionState.Open)
{
Connection.Open();
}
Command=DBManagerFactory.GetCommand(ProviderType);
}publicvoidClose()
{if(Connection.State!=ConnectionState.Closed)
{
Connection.Close();
}
}publicvoidDispose()
{
GC.SuppressFinalize(this);
Close();
Command=null;
Transaction=null;
Connection=null;
}publicvoidCreateParameters(intparamsCount)
{
Parameters=newIDbDataParameter[paramsCount];
Parameters=DBManagerFactory.GetParameters(ProviderType, paramsCount);
}publicvoidAddParameters(intindex,stringparamName,objectobjValue)
{if(index
{
Parameters[index].ParameterName=paramName;
Parameters[index].Value=objValue;
}
}publicvoidBeginTransaction()
{if(Transaction==null)
{
Transaction=DBManagerFactory.GetTransaction(ProviderType);
}
Command.Transaction=Transaction;
}publicvoidCommitTransaction()
{if(Transaction!=null)
{
Transaction.Commit();
}
Transaction=null;
}publicvoidCloseReader()
{if(DataReader!=null)
{
DataReader.Close();
}
}publicIDataReader ExecuteReader(CommandType commandType,stringcommandText)
{
Command=DBManagerFactory.GetCommand(ProviderType);
Command.Connection=Connection;
PrepareCommand(Command, Connection, Transaction, commandType, commandText, Parameters);
DataReader=Command.ExecuteReader();
Command.Parameters.Clear();returnDataReader;
}publicintExecuteNonQuery(CommandType commandType,stringcommandText)
{
Command=DBManagerFactory.GetCommand(ProviderType);
PrepareCommand(Command, Connection, Transaction, commandType, commandText, Parameters);intreturnValue=Command.ExecuteNonQuery();
Command.Parameters.Clear();returnreturnValue;
}publicobjectExecuteScalar(CommandType commandType,stringcommandText)
{
Command=DBManagerFactory.GetCommand(ProviderType);
PrepareCommand(Command, Connection, Transaction, commandType, commandText, Parameters);objectreturnValue=Command.ExecuteScalar();
Command.Parameters.Clear();returnreturnValue;
}publicDataSet ExecuteDataSet(CommandType commandType,stringcommandText)
{
Command=DBManagerFactory.GetCommand(ProviderType);
PrepareCommand(Command, Connection, Transaction, commandType, commandText, Parameters);
IDbDataAdapter dataAdapter=DBManagerFactory.GetDataAdapter(ProviderType);
dataAdapter.SelectCommand=Command;
DataSet dataSet=newDataSet();
dataAdapter.Fill(dataSet);
Command.Parameters.Clear();returndataSet;
}#endregion#region私有方法privatevoidAttachParameters(IDbCommand command, IDbDataParameter[] commandParameters)
{foreach(IDbDataParameter idbParameterincommandParameters)
{if(idbParameter.Direction==ParameterDirection.InputOutput&&idbParameter.Value==null)
{
idbParameter.Value=DBNull.Value;
}
command.Parameters.Add(idbParameter);
}
}privatevoidPrepareCommand(IDbCommand command, IDbConnection connection, IDbTransaction transaction,
CommandType commandType,stringcommandText, IDbDataParameter[] commandParameters)
{
command.Connection=connection;
command.CommandText=commandText;
command.CommandType=commandType;if(transaction!=null)
{
command.Transaction=transaction;
}if(commandParameters!=null)
{
AttachParameters(command, commandParameters);
}
}#endregion}
}