一个比较好用的DBHelper

忘记从哪里拿过来的了,使用过程中觉得还不错,故推荐给大家也用一下。

 

ContractedBlock.gif ExpandedBlockStart.gif DBHelper
    public class DBHelper
    {
        
#region private members
        
private string _connectionstring = "";
        
private DbConnection _connection;
        
private DbCommand _command;
        
private DbProviderFactory _factory = null;
        
#endregion

        
#region properties

        
/// <summary>
        
/// Gets or Sets the connection string for the database
        
/// </summary>
        public string connectionstring
        {
            
get
            {
                
return _connectionstring;
            }
            
set
            {
                
if (value != "")
                {
                    _connectionstring 
= value;
                }
            }
        }

        
/// <summary>
        
/// Gets the connection object for the database
        
/// </summary>
        public DbConnection connection
        {
            
get
            {
                
return _connection;
            }
        }

        
/// <summary>
        
/// Gets the command object for the database
        
/// </summary>
        public DbCommand command
        {
            
get
            {
                
return _command;
            }
        }

        
#endregion

        # region methods

        
/// <summary>
        
/// Determines the correct provider to use and sets up the connection and command
        
/// objects for use in other methods
        
/// </summary>
        
/// <param name="connectString">The full connection string to the database</param>
        
/// <param name="providerList">The enum value of providers from dbutilities.Providers</param>
        public void CreateDBObjects(string connectString, Providers providerList)
        {
            
//CreateDBObjects(connectString, providerList, null);
            switch (providerList)
            {
                
case Providers.SqlServer:
                    _factory 
= SqlClientFactory.Instance;
                    
break;
                
//case Providers.Oracle:
                    
//_factory = OracleClientFactory.Instance;
                    
//break;
                case Providers.OleDB:
                    _factory 
= OleDbFactory.Instance;
                    
break;
                
case Providers.ODBC:
                    _factory 
= OdbcFactory.Instance;
                    
break;
            }

            _connection 
= _factory.CreateConnection();
            _command 
= _factory.CreateCommand();

            _connection.ConnectionString 
= connectString;
            _command.Connection 
= connection;
        }

        
#region parameters

        
/// <summary>
        
/// Creates a parameter and adds it to the command object
        
/// </summary>
        
/// <param name="name">The parameter name</param>
        
/// <param name="value">The paremeter value</param>
        
/// <returns></returns>
        public int AddParameter(string name, object value)
        {
            DbParameter parm 
= _factory.CreateParameter();
            parm.ParameterName 
= name;
            parm.Value 
= value;
            
return command.Parameters.Add(parm);
        }

        
/// <summary>
        
/// Creates a parameter and adds it to the command object
        
/// </summary>
        
/// <param name="parameter">A parameter object</param>
        
/// <returns></returns>
        public int AddParameter(DbParameter parameter)
        {
            
return command.Parameters.Add(parameter);
        }

        
#endregion

        
#region transactions

        
/// <summary>
        
/// Starts a transaction for the command object
        
/// </summary>
        private void BeginTransaction()
        {
            
if (connection.State == System.Data.ConnectionState.Closed)
            {
                connection.Open();
            }
            command.Transaction 
= connection.BeginTransaction();
        }

        
/// <summary>
        
/// Commits a transaction for the command object
        
/// </summary>
        private void CommitTransaction()
        {
            command.Transaction.Commit();
            connection.Close();
        }

        
/// <summary>
        
/// Rolls back the transaction for the command object
        
/// </summary>
        private void RollbackTransaction()
        {
            command.Transaction.Rollback();
            connection.Close();
        }

        
#endregion

        
#region execute database functions

        
/// <summary>
        
/// Executes a statement that does not return a result set, such as an INSERT, UPDATE, DELETE, or a data definition statement
        
/// </summary>
        
/// <param name="query">The query, either SQL or Procedures</param>
        
/// <param name="commandtype">The command type, text, storedprocedure, or tabledirect</param>
        
/// <param name="connectionstate">The connection state</param>
        
/// <returns>An integer value</returns>
        public int ExecuteNonQuery(string query, CommandType commandtype, ConnectionState connectionstate)
        {
            command.CommandText 
= query;
            command.CommandType 
= commandtype;
            
int i = -1;
            
try
            {
                
if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }

                BeginTransaction();

                i 
= command.ExecuteNonQuery();
            }
            
catch (Exception ex)
            {
                RollbackTransaction();
                
throw (ex);
            }
            
finally
            {
                CommitTransaction();
                command.Parameters.Clear();

                
if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                    connection.Dispose();
                    
//command.Dispose();
                }
            }

            
return i;
        }

        
/// <summary>
        
/// Executes a statement that returns a single value. 
        
/// If this method is called on a query that returns multiple rows and columns, only the first column of the first row is returned.
        
/// </summary>
        
/// <param name="query">The query, either SQL or Procedures</param>
        
/// <param name="commandtype">The command type, text, storedprocedure, or tabledirect</param>
        
/// <param name="connectionstate">The connection state</param>
        
/// <returns>An object that holds the return value(s) from the query</returns>
        public object ExecuteScaler(string query, CommandType commandtype, ConnectionState connectionstate)
        {
            command.CommandText 
= query;
            command.CommandType 
= commandtype;
            
object obj = null;
            
try
            {
                
if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }

                BeginTransaction();
                obj 
= command.ExecuteScalar();
            }
            
catch (Exception ex)
            {
                RollbackTransaction();
                
throw (ex);
            }
            
finally
            {
                CommitTransaction();
                command.Parameters.Clear();

                
if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                    connection.Dispose();
                    command.Dispose();
                }
            }

            
return obj;
        }

        
/// <summary>
        
/// Executes a SQL statement that returns a result set.
        
/// </summary>
        
/// <param name="query">The query, either SQL or Procedures</param>
        
/// <param name="commandtype">The command type, text, storedprocedure, or tabledirect</param>
        
/// <param name="connectionstate">The connection state</param>
        
/// <returns>A datareader object</returns>
        public DbDataReader ExecuteReader(string query, CommandType commandtype, ConnectionState connectionstate)
        {
            command.CommandText 
= query;
            command.CommandType 
= commandtype;
            DbDataReader reader 
= null;
            
try
            {
                
if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                
if (connectionstate == System.Data.ConnectionState.Open)
                {
                    reader 
= command.ExecuteReader(CommandBehavior.CloseConnection);
                }
                
else
                {
                    reader 
= command.ExecuteReader();
                }
            }
            
catch (Exception ex)
            {
                
throw (ex);
            }
            
finally
            {
                command.Parameters.Clear();
            }

            
return reader;
        }

        
/// <summary>
        
/// Generates a dataset
        
/// </summary>
        
/// <param name="query">The query, either SQL or Procedures</param>
        
/// <param name="commandtype">The command type, text, storedprocedure, or tabledirect</param>
        
/// <param name="connectionstate">The connection state</param>
        
/// <returns>A dataset containing data from the database</returns>
        public DataSet GetDataSet(string query, CommandType commandtype, ConnectionState connectionstate)
        {
            DbDataAdapter adapter 
= _factory.CreateDataAdapter();
            command.CommandText 
= query;
            command.CommandType 
= commandtype;
            adapter.SelectCommand 
= command;
            DataSet ds 
= new DataSet();
            
try
            {
                adapter.Fill(ds);
            }
            
catch (Exception ex)
            {
                
throw ex;
            }
            
finally
            {
                command.Parameters.Clear();

                
if (connection.State == System.Data.ConnectionState.Open)
                {
                    connection.Close();
                    connection.Dispose();
                    command.Dispose();
                }
            }
            
return ds;
        }

        
#endregion

        
#endregion

        
#region enums

        
/// <summary>
        
/// A list of data providers
        
/// </summary>
        public enum Providers
        {
            SqlServer,
            OleDB,
            ODBC,
            
//Oracle,
        }

        
#endregion
    }

转载于:https://www.cnblogs.com/seamusic/archive/2009/01/16/1377190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值