linxi27的专栏

好好學習dotnet

BDhelper数据库操作类
我在前一篇文章中谈到了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;

namespace namespace1
...{
    
public class DbHelper
    
...{
        
private static string dbProviderName;

        
private DbConnection connection;

        
public DbHelper()
        
...{
            
this.connection = CreateConnection();
        }


        
public DbHelper(string cfgConnectionString)
        
...{
            
this.connection = CreateConnection(cfgConnectionString);
        }


        
public static DbConnection CreateConnection()
        
...{
            
return CreateConnection("connectionString");
        }


        
public static DbConnection CreateConnection(string cfgConnectionString)
        
...{            
            dbProviderName 
= ConfigurationManager.ConnectionStrings[cfgConnectionString].ProviderName;
            DbProviderFactory dbfactory 
= DbProviderFactories.GetFactory(dbProviderName);
            DbConnection dbconn 
= dbfactory.CreateConnection();
            dbconn.ConnectionString 
= ConfigurationManager.ConnectionStrings[cfgConnectionString].ConnectionString;
            
return dbconn;
        }


        
public DbCommand GetStoredProcCommand(string storedProcedure)
        
...{
            DbCommand dbCommand 
= connection.CreateCommand();
            dbCommand.CommandText 
= storedProcedure;
            dbCommand.CommandType 
= CommandType.StoredProcedure;
            
return dbCommand;
        }

        
public DbCommand GetSqlStringCommand(string sqlQuery)
        
...{
            DbCommand dbCommand 
= connection.CreateCommand();
            dbCommand.CommandText 
= sqlQuery;
            dbCommand.CommandType 
= CommandType.Text;
            
return dbCommand;
        }


        增加参数
增加参数#region 增加参数

        
public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
        
...{
            
foreach (DbParameter dbParameter in dbParameterCollection)
            
...{
                cmd.Parameters.Add(dbParameter);
            }

        }


        
public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
        
...{
            DbParameter dbParameter 
= cmd.CreateParameter();
            dbParameter.DbType 
= dbType;
            dbParameter.ParameterName 
= parameterName;
            dbParameter.Size 
= size;
            dbParameter.Direction 
= ParameterDirection.Output;
            cmd.Parameters.Add(dbParameter);
        }


        
public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
        
...{
            DbParameter dbParameter 
= cmd.CreateParameter();
            dbParameter.DbType 
= dbType;
            dbParameter.ParameterName 
= parameterName;
            dbParameter.Value 
= value;
            dbParameter.Direction 
= ParameterDirection.Input;
            cmd.Parameters.Add(dbParameter);
        }


        
public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
        
...{
            DbParameter dbParameter 
= cmd.CreateParameter();
            dbParameter.DbType 
= dbType;
            dbParameter.ParameterName 
= parameterName;
            dbParameter.Direction 
= ParameterDirection.ReturnValue;
            cmd.Parameters.Add(dbParameter);
        }


        
public DbParameter GetParameter(DbCommand cmd, string parameterName)
        
...{
            
return cmd.Parameters[parameterName];
        }


        
#endregion


        执行
执行#region 执行

        
public DataSet ExecuteDataSet(DbCommand cmd)
        
...{
            DbProviderFactory dbfactory 
= DbProviderFactories.GetFactory(DbHelper.dbProviderName);
            DbDataAdapter dbDataAdapter 
= dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand 
= cmd;
            DataSet ds 
= new DataSet();
            dbDataAdapter.Fill(ds);
            
return ds;
        }


        
public DataTable ExecuteDataTable(DbCommand cmd)
        
...{
            DbProviderFactory dbfactory 
= DbProviderFactories.GetFactory(DbHelper.dbProviderName);
            DbDataAdapter dbDataAdapter 
= dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand 
= cmd;
            DataTable dataTable 
= new DataTable();
            dbDataAdapter.Fill(dataTable);
            
return dataTable;
        }


        
public DbDataReader ExecuteReader(DbCommand cmd)
        
...{
            cmd.Connection.Open();
            DbDataReader reader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
            
return reader;
        }

        
public int ExecuteNonQuery(DbCommand cmd)
        
...{
            cmd.Connection.Open();
            
int ret = cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            
return ret;
        }


        
public object ExecuteScalar(DbCommand cmd)
        
...{
            cmd.Connection.Open();
            
object ret = cmd.ExecuteScalar();
            cmd.Connection.Close();
            
return ret;
        }

        
#endregion


        执行事务
执行事务#region 执行事务

        
public DataSet ExecuteDataSet(DbCommand cmd, Transaction t)
        
...{
            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;
        }


        
public DataTable ExecuteDataTable(DbCommand cmd, Transaction t)
        
...{
            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;
        }


        
public DbDataReader ExecuteReader(DbCommand cmd, Transaction t)
        
...{
            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)
        
...{
            cmd.Connection.Close();
            cmd.Connection 
= t.DbConnection;
            cmd.Transaction 
= t.DbTrans;
            
int ret = cmd.ExecuteNonQuery();
            
return ret;
        }


        
public object ExecuteScalar(DbCommand cmd, Transaction t)
        
...{
            cmd.Connection.Close();
            cmd.Connection 
= t.DbConnection;
            cmd.Transaction 
= t.DbTrans;
            
object ret = cmd.ExecuteScalar();
            
return ret;
        }

        
#endregion

    }


    
public class Transaction : IDisposable
    
...{
        
private DbConnection conn;
        
private DbTransaction dbTrans;

        
public DbConnection DbConnection
        
...{
            
get ...return this.conn; }
        }


        
public DbTransaction DbTrans
        
...{
            
get ...return this.dbTrans; }
        }


        
public Transaction()
        
...{
            conn 
= DbHelper.CreateConnection();
            conn.Open();
            dbTrans 
= conn.BeginTransaction();
        }


        
public Transaction(string cfgConnectionString)
        
...{
            conn 
= DbHelper.CreateConnection(cfgConnectionString);
            conn.Open();
            dbTrans 
= conn.BeginTransaction();
        }


        
public void Commit()
        
...{
            dbTrans.Commit();
            
this.Colse();
        }


        
public void RollBack()
        
...{
            dbTrans.Rollback();
            
this.Colse();
        }


        
public void Dispose()
        
...{
            
this.Colse();
        }


        
public void Colse()
        
...{
            
if (conn.State == System.Data.ConnectionState.Open)
            
...{
                conn.Close();
            }

        }

    }

}

 
阅读更多
想对作者说点什么? 我来说一句

dbhelper类封装增删查改

2015年07月06日 13KB 下载

asp.net数据库操作

2009年08月18日 2.9MB 下载

C#.net数据库操作

2009年09月08日 33KB 下载

C# windows窗体学生系统

2018年03月29日 1.18MB 下载

SQL数据库操作类(C#)

2010年05月25日 1KB 下载

php数据库操作类 DbClass

2010年05月18日 5KB 下载

C#数据库操作

2007年09月14日 9KB 下载

DbHelper数据库操作

2010年07月12日 7KB 下载

PHP adodb5 数据库操作

2011年05月27日 647KB 下载

没有更多推荐了,返回首页

不良信息举报

BDhelper数据库操作类

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭