关闭

BDhelper数据库操作类

438人阅读 评论(0) 收藏 举报
我在前一篇文章中谈到了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;
        }


        增加参数
增加参数

        执行
执行

        执行事务
执行事务
    }


    
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();
            }

        }

    }

}

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63366次
    • 积分:977
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:17篇
    • 译文:0篇
    • 评论:13条
    文章分类