我在前一篇文章中谈到了DbHelper这一个访问数据库的基础类,在用过之后发现有些地方还要修改一下才更加的完善。
首先应该把dbProviderName ,dbConnectionString 这两个static的字段放到默认的构造函数中,不然当你用自定义的连接字符串初始化类DbHelper db=new DbHelper("otherConnectionString"),而配置文件中又没有"connecionString"连接字符串时就会出错;当然这时改过后如果用默认的构造函数创建类也会出错,因为默认的是要有"connectionString"这一数据库连接字符串。后来又发觉既然配置文件中有connectionStrings这一配置节,为什么不把dbProviderName ,dbConnectionString 放在里面呢?
最后整理如下:
using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace namespace1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class DbHelper
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private static string dbProviderName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DbConnection connection;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbHelper()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.connection = CreateConnection();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbHelper(string cfgConnectionString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.connection = CreateConnection(cfgConnectionString);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DbConnection CreateConnection()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return CreateConnection("connectionString");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static DbConnection CreateConnection(string cfgConnectionString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dbProviderName = ConfigurationManager.ConnectionStrings[cfgConnectionString].ProviderName;
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(dbProviderName);
DbConnection dbconn = dbfactory.CreateConnection();
dbconn.ConnectionString = ConfigurationManager.ConnectionStrings[cfgConnectionString].ConnectionString;
return dbconn;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbCommand GetStoredProcCommand(string storedProcedure)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = storedProcedure;
dbCommand.CommandType = CommandType.StoredProcedure;
return dbCommand;
}
public DbCommand GetSqlStringCommand(string sqlQuery)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = sqlQuery;
dbCommand.CommandType = CommandType.Text;
return dbCommand;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
增加参数#region 增加参数
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (DbParameter dbParameter in dbParameterCollection)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Parameters.Add(dbParameter);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Size = size;
dbParameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(dbParameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Value = value;
dbParameter.Direction = ParameterDirection.Input;
cmd.Parameters.Add(dbParameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(dbParameter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbParameter GetParameter(DbCommand cmd, string parameterName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return cmd.Parameters[parameterName];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
执行#region 执行
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataSet ExecuteDataSet(DbCommand cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();
dbDataAdapter.Fill(ds);
return ds;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataTable ExecuteDataTable(DbCommand cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataTable dataTable = new DataTable();
dbDataAdapter.Fill(dataTable);
return dataTable;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbDataReader ExecuteReader(DbCommand cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection.Open();
DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
public int ExecuteNonQuery(DbCommand cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection.Open();
int ret = cmd.ExecuteNonQuery();
cmd.Connection.Close();
return ret;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public object ExecuteScalar(DbCommand cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection.Open();
object ret = cmd.ExecuteScalar();
cmd.Connection.Close();
return ret;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
执行事务#region 执行事务
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataSet ExecuteDataSet(DbCommand cmd, Transaction t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();
dbDataAdapter.Fill(ds);
return ds;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataTable ExecuteDataTable(DbCommand cmd, Transaction t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataTable dataTable = new DataTable();
dbDataAdapter.Fill(dataTable);
return dataTable;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbDataReader ExecuteReader(DbCommand cmd, Transaction t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection.Close();
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
DbDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
return reader;
}
public int ExecuteNonQuery(DbCommand cmd, Transaction t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection.Close();
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
int ret = cmd.ExecuteNonQuery();
return ret;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public object ExecuteScalar(DbCommand cmd, Transaction t)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cmd.Connection.Close();
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
object ret = cmd.ExecuteScalar();
return ret;
}
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class Transaction : IDisposable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private DbConnection conn;
private DbTransaction dbTrans;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbConnection DbConnection
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.conn; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DbTransaction DbTrans
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return this.dbTrans; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Transaction()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn = DbHelper.CreateConnection();
conn.Open();
dbTrans = conn.BeginTransaction();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Transaction(string cfgConnectionString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn = DbHelper.CreateConnection(cfgConnectionString);
conn.Open();
dbTrans = conn.BeginTransaction();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Commit()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dbTrans.Commit();
this.Colse();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void RollBack()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dbTrans.Rollback();
this.Colse();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Colse();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Colse()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (conn.State == System.Data.ConnectionState.Open)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Close();
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
还是放个文件上来吧:
下载