通用的数据库操作类(支持多种数据库)

 通用的数据库操作类(支持多种数据库)

      我们在软件开发过程中,数据库是一个永久不变的话题,但是根据软件架构与客户要求的不同我们会选择不同的数据库,在C#中不同数据库操作编写代码不尽相同,下面提供一种通用的数据库操作方案,只需要根据config的配置就可以动态的选择不同的数据库.

在配置文件中providerName指定不同的数据库类型.

<connectionStrings>
        <add name="ConnectionString"  connectionString=" ..." providerName="System.Data.OleDb" />

        <add name="ConnectionString"  connectionString=" ..." providerName="System.Data.SqlClient" />
 </connectionStrings>

代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Data.Common;

namespace DataBase
{
    
/// <summary>
    
/// 数据访问基础类
    
/// </summary>
    class DataHelper
    {
        
protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        DbProviderFactory provider;
        
public DataHelper()
        {
            provider 
= DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName);
        }

        
#region  执行简单SQL语句

        
/// <summary>
        
/// 执行SQL语句,返回影响的记录数
        
/// </summary>
        
/// <param name="SQLString">SQL语句</param>
        
/// <returns>影响的记录数</returns>
        public int ExecuteSql(string SQLString)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    cmd.Connection 
= connection;
                    cmd.CommandText 
= SQLString;
                    
try
                    {
                        connection.Open();
                        
int rows = cmd.ExecuteNonQuery();
                        
return rows;
                    }
                    
catch (DbException E)
                    {
                        connection.Close();
                        connection.Dispose();
                        
throw new Exception(E.Message);
                    }
                }
            }
        }

        
/// <summary>
        
/// 执行多条SQL语句,实现数据库事务。
        
/// </summary>
        
/// <param name="SQLStringList">多条SQL语句</param>        
        public void ExecuteSqlTran(ArrayList SQLStringList)
        {
            
using (DbConnection conn = provider.CreateConnection())
            {
                conn.ConnectionString 
= connectionString;
                conn.Open();
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    cmd.Connection 
= conn;
                    
using (DbTransaction tx = conn.BeginTransaction())
                    {
                        cmd.Transaction 
= tx;
                        
try
                        {
                            
for (int n = 0; n < SQLStringList.Count; n++)
                            {
                                
string strsql = SQLStringList[n].ToString();
                                
if (strsql.Trim().Length > 1)
                                {
                                    cmd.CommandText 
= strsql;
                                    cmd.ExecuteNonQuery();
                                }
                            }
                            tx.Commit();
                        }
                        
catch (DbException ex)
                        {
                            tx.Rollback();
                            conn.Close();
                            conn.Dispose();
                            
throw ex;
                        }
                    }
                }
            }
        }
        
/// <summary>
        
/// 执行一条计算查询结果语句,返回查询结果(object)。
        
/// </summary>
        
/// <param name="SQLString">计算查询结果语句</param>
        
/// <returns>查询结果(object)</returns>
        public object GetSingle(string SQLString)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    cmd.Connection 
= connection;
                    cmd.CommandText 
= SQLString;
                    
try
                    {
                        connection.Open();
                        
object obj = cmd.ExecuteScalar();
                        
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            
return null;
                        }
                        
else
                        {
                            
return obj;
                        }
                    }
                    
catch (DbException e)
                    {
                        connection.Close();
                        connection.Dispose();
                        
throw new Exception(e.Message);
                    }
                }
            }
        }
        
/// <summary>
        
/// 执行查询语句,返回SqlDataReader
        
/// </summary>
        
/// <param name="strSQL">查询语句</param>
        
/// <returns>SqlDataReader</returns>
        public DbDataReader ExecuteReader(string strSQL)
        {
            DbConnection connection 
= provider.CreateConnection();
            connection.ConnectionString 
= connectionString;
            DbCommand cmd 
= provider.CreateCommand();
            cmd.Connection 
= connection;
            cmd.CommandText 
= strSQL;
            
try
            {
                connection.Open();
                DbDataReader myReader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
                
return myReader;
            }
            
catch (System.Data.Common.DbException e)
            {
                connection.Close();
                connection.Dispose();
                
throw new Exception(e.Message);
            }

        }
        
/// <summary>
        
/// 执行查询语句,返回DataSet
        
/// </summary>
        
/// <param name="SQLString">查询语句</param>
        
/// <returns>DataSet</returns>
        public DataSet GetDataSet(string SQLString)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    cmd.Connection 
= connection;
                    cmd.CommandText 
= SQLString;
                    
try
                    {
                        DataSet ds 
= new DataSet();
                        DbDataAdapter adapter 
= provider.CreateDataAdapter();
                        adapter.SelectCommand 
= cmd;
                        adapter.Fill(ds, 
"ds");
                        
return ds;
                    }
                    
catch (DbException ex)
                    {
                        connection.Close();
                        connection.Dispose();
                        
throw new Exception(ex.Message);
                    }
                }
            }
        }
        
#endregion

        
#region 执行带参数的SQL语句

        
/// <summary>
        
/// 执行SQL语句,返回影响的记录数
        
/// </summary>
        
/// <param name="SQLString">SQL语句</param>
        
/// <returns>影响的记录数</returns>
        public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    cmd.Connection 
= connection;
                    cmd.CommandText 
= SQLString;
                    
try
                    {
                        PrepareCommand(cmd, connection, 
null, SQLString, cmdParms);
                        
int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        
return rows;
                    }
                    
catch (DbException E)
                    {
                        connection.Close();
                        connection.Dispose();
                        
throw new Exception(E.Message);
                    }
                }
            }
        }

        
/// <summary>
        
/// 执行多条SQL语句,实现数据库事务。
        
/// </summary>
        
/// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
        public void ExecuteSqlTran(Hashtable SQLStringList)
        {
            
using (DbConnection conn = provider.CreateConnection())
            {
                conn.ConnectionString 
= connectionString;
                conn.Open();
                
using (DbTransaction trans = conn.BeginTransaction())
                {
                    
using (DbCommand cmd = provider.CreateCommand())
                    {
                        
try
                        {
                            
//循环
                            foreach (DictionaryEntry myDE in SQLStringList)
                            {
                                
string cmdText = myDE.Key.ToString();
                                DbParameter[] cmdParms 
= (DbParameter[])myDE.Value;
                                PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                                
int val = cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            trans.Commit();
                        }
                        
catch (DbException ex)
                        {
                            trans.Rollback();
                            conn.Close();
                            conn.Dispose();
                            
throw ex;
                        }
                    }
                }
            }
        }

        
/// <summary>
        
/// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
        
/// </summary>
        
/// <param name="SQLString">计算查询结果语句</param>
        
/// <returns>查询结果(object)</returns>
        public object GetSingle(string SQLString, DbParameter[] cmdParms)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    
try
                    {
                        PrepareCommand(cmd, connection, 
null, SQLString, cmdParms);
                        
object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            
return null;
                        }
                        
else
                        {
                            
return obj;
                        }
                    }
                    
catch (DbException e)
                    {
                        connection.Close();
                        connection.Dispose();
                        
throw new Exception(e.Message);
                    }
                }
            }
        }

        
/// <summary>
        
/// 执行查询语句,返回SqlDataReader
        
/// </summary>
        
/// <param name="strSQL">查询语句</param>
        
/// <returns>SqlDataReader</returns>
        public DbDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
        {
            DbConnection connection 
= provider.CreateConnection();
            connection.ConnectionString 
= connectionString;
            DbCommand cmd 
= provider.CreateCommand();
            
try
            {
                PrepareCommand(cmd, connection, 
null, SQLString, cmdParms);
                DbDataReader myReader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                
return myReader;
            }
            
catch (DbException e)
            {
                connection.Close();
                connection.Dispose();
                
throw new Exception(e.Message);
            }

        }

        
/// <summary>
        
/// 执行查询语句,返回DataSet
        
/// </summary>
        
/// <param name="SQLString">查询语句</param>
        
/// <returns>DataSet</returns>
        public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
using (DbCommand cmd = provider.CreateCommand())
                {
                    
using (DbDataAdapter da = provider.CreateDataAdapter())
                    {
                        PrepareCommand(cmd, connection, 
null, SQLString, cmdParms);
                        da.SelectCommand 
= cmd;
                        DataSet ds 
= new DataSet();
                        
try
                        {
                            da.Fill(ds, 
"ds");
                            cmd.Parameters.Clear();
                            
return ds;
                        }
                        
catch (DbException ex)
                        {
                            connection.Close();
                            connection.Dispose();
                            
throw new Exception(ex.Message);
                        }
                    }
                }
            }
        }

        
private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, string cmdText, DbParameter[] cmdParms)
        {
            
if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection 
= conn;
            cmd.CommandText 
= cmdText;
            
if (trans != null)
            {
                cmd.Transaction 
= trans;
            }
            cmd.CommandType 
= CommandType.Text;//cmdType;
            if (cmdParms != null)
            {
                
foreach (DbParameter parm in cmdParms)
                {
                    cmd.Parameters.Add(parm);
                }
            }
        }

        
#endregion

        
#region 存储过程操作
        
/// <summary>
        
/// 执行存储过程;
        
/// </summary>
        
/// <param name="storeProcName">存储过程名</param>
        
/// <param name="parameters">所需要的参数</param>
        
/// <returns>返回受影响的行数</returns>
        public int RunProcedureExecuteSql(string storeProcName, DbParameter[] parameters)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                DbCommand cmd 
= BuildQueryCommand(connection, storeProcName, parameters);
                
int rows = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                connection.Close();
                
return rows;
            }
        }

        
/// <summary>
        
/// 执行存储过程,返回首行首列的值
        
/// </summary>
        
/// <param name="storeProcName">存储过程名</param>
        
/// <param name="parameters">存储过程参数</param>
        
/// <returns>返回首行首列的值</returns>
        public Object RunProcedureGetSingle(string storeProcName, DbParameter[] parameters)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                
try
                {
                    DbCommand cmd 
= BuildQueryCommand(connection, storeProcName, parameters);
                    
object obj = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                    {
                        
return null;
                    }
                    
else
                    {
                        
return obj;
                    }
                }
                
catch (DbException e)
                {
                    connection.Close();
                    connection.Dispose();
                    
throw new Exception(e.Message);
                }
            }
        }

        
/// <summary>
        
/// 执行存储过程
        
/// </summary>
        
/// <param name="storedProcName">存储过程名</param>
        
/// <param name="parameters">存储过程参数</param>
        
/// <returns>SqlDataReader</returns>
        public DbDataReader RunProcedureGetDataReader(string storedProcName, DbParameter[] parameters)
        {
            DbConnection connection 
= provider.CreateConnection();
            connection.ConnectionString 
= connectionString;
            DbDataReader returnReader;
            DbCommand cmd 
= BuildQueryCommand(connection, storedProcName, parameters);
            cmd.CommandType 
= CommandType.StoredProcedure;
            returnReader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
            cmd.Parameters.Clear();
            
return returnReader;
        }

        
/// <summary>
        
/// 执行存储过程
        
/// </summary>
        
/// <param name="storedProcName">存储过程名</param>
        
/// <param name="parameters">存储过程参数</param>
        
/// <returns>DataSet</returns>
        public DataSet RunProcedureGetDataSet(string storedProcName, DbParameter[] parameters)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                DataSet dataSet 
= new DataSet();
                DbDataAdapter sqlDA 
= provider.CreateDataAdapter();
                sqlDA.SelectCommand 
= BuildQueryCommand(connection, storedProcName, parameters);
                sqlDA.Fill(dataSet);
                sqlDA.SelectCommand.Parameters.Clear();
                sqlDA.Dispose();
                
return dataSet;
            }
        }

        
/// <summary>
        
/// 执行多个存储过程,实现数据库事务。
        
/// </summary>
        
/// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
        public bool RunProcedureTran(Hashtable SQLStringList)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                connection.Open();
                
using (DbTransaction trans = connection.BeginTransaction())
                {
                    
using (DbCommand cmd = provider.CreateCommand())
                    {
                        
try
                        {
                            
//循环
                            foreach (DictionaryEntry myDE in SQLStringList)
                            {
                                cmd.Connection 
= connection;
                                
string storeName = myDE.Value.ToString();
                                DbParameter[] cmdParms 
= (DbParameter[])myDE.Key;

                                cmd.Transaction 
= trans;
                                cmd.CommandText 
= storeName;
                                cmd.CommandType 
= CommandType.StoredProcedure;
                                
if (cmdParms != null)
                                {
                                    
foreach (DbParameter parameter in cmdParms)
                                    {
                                        cmd.Parameters.Add(parameter);
                                    }
                                }
                                
int val = cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            trans.Commit();
                            
return true;
                        }
                        
catch
                        {
                            trans.Rollback();
                            connection.Close();
                            connection.Dispose(); 
                            
return false;
                        }
                    }
                }
            }
        }

        
/// <summary>
        
/// 执行多个存储过程,实现数据库事务。
        
/// </summary>
        
/// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
        public bool RunProcedureTran(C_HashTable SQLStringList)
        {
            
using (DbConnection connection = provider.CreateConnection())
            {
                connection.ConnectionString 
= connectionString;
                connection.Open();
                
using (DbTransaction trans = connection.BeginTransaction())
                {
                    
using (DbCommand cmd = provider.CreateCommand())
                    {
                        
try
                        {
                            
//循环
                            foreach (DbParameter[] cmdParms in SQLStringList.Keys)
                            {
                                cmd.Connection 
= connection;
                                
string storeName = SQLStringList[cmdParms].ToString();

                                cmd.Transaction 
= trans;
                                cmd.CommandText 
= storeName;
                                cmd.CommandType 
= CommandType.StoredProcedure;
                                
if (cmdParms != null)
                                {
                                    
foreach (DbParameter parameter in cmdParms)
                                    {
                                        cmd.Parameters.Add(parameter);
                                    }
                                }
                                
int val = cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                            }
                            trans.Commit();
                            
return true;
                        }
                        
catch
                        {
                            trans.Rollback();
                            connection.Close();
                            connection.Dispose();
                            
return false;                            
                        }
                    }
                }
            }
        }

        
/// <summary>
        
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
        
/// </summary>
        
/// <param name="connection">数据库连接</param>
        
/// <param name="storedProcName">存储过程名</param>
        
/// <param name="parameters">存储过程参数</param>
        
/// <returns>SqlCommand</returns>
        private DbCommand BuildQueryCommand(DbConnection connection, string storedProcName, DbParameter[] parameters)
        {
            
if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
            DbCommand command 
= provider.CreateCommand();
            command.CommandText 
= storedProcName;
            command.Connection 
= connection;
            command.CommandType 
= CommandType.StoredProcedure;
            
if (parameters != null)
            {
                
foreach (DbParameter parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }
            }
            
return command;
        }
        
#endregion
    }
}
ing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Collections.Specialized;
  4None.gifusing System.Data;
  5None.gifusing System.Configuration;
  6None.gifusing System.Data.Common;
  7None.gif
  8None.gifnamespace DataBase
  9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11InBlock.gif    /// 数据访问基础类(基于SQLServer)
 12ExpandedSubBlockEnd.gif    /// </summary>

 13InBlock.gif    class DataHelper
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 16InBlock.gif        DbProviderFactory provider;
 17InBlock.gif        public DataHelper()
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            provider = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName);
 20ExpandedSubBlockEnd.gif        }

 21InBlock.gif
 22ContractedSubBlock.gifExpandedSubBlockStart.gif        执行简单SQL语句#region  执行简单SQL语句
 23InBlock.gif
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 25InBlock.gif        /// 执行SQL语句,返回影响的记录数
 26InBlock.gif        /// </summary>
 27InBlock.gif        /// <param name="SQLString">SQL语句</param>
 28ExpandedSubBlockEnd.gif        /// <returns>影响的记录数</returns>

 29InBlock.gif        public int ExecuteSql(string SQLString)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            using (DbConnection connection = provider.CreateConnection())
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 33InBlock.gif                connection.ConnectionString = connectionString;
 34InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 36InBlock.gif                    cmd.Connection = connection;
 37InBlock.gif                    cmd.CommandText = SQLString;
 38InBlock.gif                    try
 39ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 40InBlock.gif                        connection.Open();
 41InBlock.gif                        int rows = cmd.ExecuteNonQuery();
 42InBlock.gif                        return rows;
 43ExpandedSubBlockEnd.gif                    }

 44InBlock.gif                    catch (DbException E)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 46InBlock.gif                        connection.Close();
 47InBlock.gif                        connection.Dispose();
 48InBlock.gif                        throw new Exception(E.Message);
 49ExpandedSubBlockEnd.gif                    }

 50ExpandedSubBlockEnd.gif                }

 51ExpandedSubBlockEnd.gif            }

 52ExpandedSubBlockEnd.gif        }

 53InBlock.gif
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 55InBlock.gif        /// 执行多条SQL语句,实现数据库事务。
 56InBlock.gif        /// </summary>
 57ExpandedSubBlockEnd.gif        /// <param name="SQLStringList">多条SQL语句</param>        

 58InBlock.gif        public void ExecuteSqlTran(ArrayList SQLStringList)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            using (DbConnection conn = provider.CreateConnection())
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif                conn.ConnectionString = connectionString;
 63InBlock.gif                conn.Open();
 64InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
 65ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 66InBlock.gif                    cmd.Connection = conn;
 67InBlock.gif                    using (DbTransaction tx = conn.BeginTransaction())
 68ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 69InBlock.gif                        cmd.Transaction = tx;
 70InBlock.gif                        try
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 72InBlock.gif                            for (int n = 0; n < SQLStringList.Count; n++)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
 74InBlock.gif                                string strsql = SQLStringList[n].ToString();
 75InBlock.gif                                if (strsql.Trim().Length > 1)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
 77InBlock.gif                                    cmd.CommandText = strsql;
 78InBlock.gif                                    cmd.ExecuteNonQuery();
 79ExpandedSubBlockEnd.gif                                }

 80ExpandedSubBlockEnd.gif                            }

 81InBlock.gif                            tx.Commit();
 82ExpandedSubBlockEnd.gif                        }

 83InBlock.gif                        catch (DbException ex)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 85InBlock.gif                            tx.Rollback();
 86InBlock.gif                            conn.Close();
 87InBlock.gif                            conn.Dispose();
 88InBlock.gif                            throw ex;
 89ExpandedSubBlockEnd.gif                        }

 90ExpandedSubBlockEnd.gif                    }

 91ExpandedSubBlockEnd.gif                }

 92ExpandedSubBlockEnd.gif            }

 93ExpandedSubBlockEnd.gif        }

 94ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 95InBlock.gif        /// 执行一条计算查询结果语句,返回查询结果(object)。
 96InBlock.gif        /// </summary>
 97InBlock.gif        /// <param name="SQLString">计算查询结果语句</param>
 98ExpandedSubBlockEnd.gif        /// <returns>查询结果(object)</returns>

 99InBlock.gif        public object GetSingle(string SQLString)
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            using (DbConnection connection = provider.CreateConnection())
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                connection.ConnectionString = connectionString;
104InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
105ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
106InBlock.gif                    cmd.Connection = connection;
107InBlock.gif                    cmd.CommandText = SQLString;
108InBlock.gif                    try
109ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
110InBlock.gif                        connection.Open();
111InBlock.gif                        object obj = cmd.ExecuteScalar();
112InBlock.gif                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
113ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
114InBlock.gif                            return null;
115ExpandedSubBlockEnd.gif                        }

116InBlock.gif                        else
117ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
118InBlock.gif                            return obj;
119ExpandedSubBlockEnd.gif                        }

120ExpandedSubBlockEnd.gif                    }

121InBlock.gif                    catch (DbException e)
122ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
123InBlock.gif                        connection.Close();
124InBlock.gif                        connection.Dispose();
125InBlock.gif                        throw new Exception(e.Message);
126ExpandedSubBlockEnd.gif                    }

127ExpandedSubBlockEnd.gif                }

128ExpandedSubBlockEnd.gif            }

129ExpandedSubBlockEnd.gif        }

130ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
131InBlock.gif        /// 执行查询语句,返回SqlDataReader
132InBlock.gif        /// </summary>
133InBlock.gif        /// <param name="strSQL">查询语句</param>
134ExpandedSubBlockEnd.gif        /// <returns>SqlDataReader</returns>

135InBlock.gif        public DbDataReader ExecuteReader(string strSQL)
136ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
137InBlock.gif            DbConnection connection = provider.CreateConnection();
138InBlock.gif            connection.ConnectionString = strSQL;
139InBlock.gif            DbCommand cmd = provider.CreateCommand();
140InBlock.gif            cmd.Connection = connection;
141InBlock.gif            cmd.CommandText = strSQL;
142InBlock.gif            try
143ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
144InBlock.gif                connection.Open();
145InBlock.gif                DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
146InBlock.gif                return myReader;
147ExpandedSubBlockEnd.gif            }

148InBlock.gif            catch (System.Data.Common.DbException e)
149ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
150InBlock.gif                connection.Close();
151InBlock.gif                connection.Dispose();
152InBlock.gif                throw new Exception(e.Message);
153ExpandedSubBlockEnd.gif            }

154InBlock.gif
155ExpandedSubBlockEnd.gif        }

156ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
157InBlock.gif        /// 执行查询语句,返回DataSet
158InBlock.gif        /// </summary>
159InBlock.gif        /// <param name="SQLString">查询语句</param>
160ExpandedSubBlockEnd.gif        /// <returns>DataSet</returns>

161InBlock.gif        public DataSet GetDataSet(string SQLString)
162ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
163InBlock.gif            using (DbConnection connection = provider.CreateConnection())
164ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
165InBlock.gif                connection.ConnectionString = connectionString;
166InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
167ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
168InBlock.gif                    cmd.Connection = connection;
169InBlock.gif                    cmd.CommandText = SQLString;
170InBlock.gif                    try
171ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
172InBlock.gif                        DataSet ds = new DataSet();
173InBlock.gif                        DbDataAdapter adapter = provider.CreateDataAdapter();
174InBlock.gif                        adapter.SelectCommand = cmd;
175InBlock.gif                        adapter.Fill(ds, "ds");
176InBlock.gif                        return ds;
177ExpandedSubBlockEnd.gif                    }

178InBlock.gif                    catch (DbException ex)
179ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
180InBlock.gif                        connection.Close();
181InBlock.gif                        connection.Dispose();
182InBlock.gif                        throw new Exception(ex.Message);
183ExpandedSubBlockEnd.gif                    }

184ExpandedSubBlockEnd.gif                }

185ExpandedSubBlockEnd.gif            }

186ExpandedSubBlockEnd.gif        }

187ExpandedSubBlockEnd.gif        #endregion

188InBlock.gif
189ContractedSubBlock.gifExpandedSubBlockStart.gif        执行带参数的SQL语句#region 执行带参数的SQL语句
190InBlock.gif
191ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
192InBlock.gif        /// 执行SQL语句,返回影响的记录数
193InBlock.gif        /// </summary>
194InBlock.gif        /// <param name="SQLString">SQL语句</param>
195ExpandedSubBlockEnd.gif        /// <returns>影响的记录数</returns>

196InBlock.gif        public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
197ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
198InBlock.gif            using (DbConnection connection = provider.CreateConnection())
199ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
200InBlock.gif                connection.ConnectionString = connectionString;
201InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
202ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
203InBlock.gif                    cmd.Connection = connection;
204InBlock.gif                    cmd.CommandText = SQLString;
205InBlock.gif                    try
206ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
207InBlock.gif                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
208InBlock.gif                        int rows = cmd.ExecuteNonQuery();
209InBlock.gif                        cmd.Parameters.Clear();
210InBlock.gif                        return rows;
211ExpandedSubBlockEnd.gif                    }

212InBlock.gif                    catch (DbException E)
213ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
214InBlock.gif                        connection.Close();
215InBlock.gif                        connection.Dispose();
216InBlock.gif                        throw new Exception(E.Message);
217ExpandedSubBlockEnd.gif                    }

218ExpandedSubBlockEnd.gif                }

219ExpandedSubBlockEnd.gif            }

220ExpandedSubBlockEnd.gif        }

221InBlock.gif
222ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
223InBlock.gif        /// 执行多条SQL语句,实现数据库事务。
224InBlock.gif        /// </summary>
225ExpandedSubBlockEnd.gif        /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>

226InBlock.gif        public void ExecuteSqlTran(Hashtable SQLStringList)
227ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
228InBlock.gif            using (DbConnection conn = provider.CreateConnection())
229ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
230InBlock.gif                conn.ConnectionString = connectionString;
231InBlock.gif                conn.Open();
232InBlock.gif                using (DbTransaction trans = conn.BeginTransaction())
233ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
234InBlock.gif                    using (DbCommand cmd = provider.CreateCommand())
235ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
236InBlock.gif                        try
237ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
238InBlock.gif                            //循环
239InBlock.gif                            foreach (DictionaryEntry myDE in SQLStringList)
240ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
241InBlock.gif                                string cmdText = myDE.Key.ToString();
242InBlock.gif                                DbParameter[] cmdParms = (DbParameter[])myDE.Value;
243InBlock.gif                                PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
244InBlock.gif                                int val = cmd.ExecuteNonQuery();
245InBlock.gif                                cmd.Parameters.Clear();
246ExpandedSubBlockEnd.gif                            }

247InBlock.gif                            trans.Commit();
248ExpandedSubBlockEnd.gif                        }

249InBlock.gif                        catch (DbException ex)
250ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
251InBlock.gif                            trans.Rollback();
252InBlock.gif                            conn.Close();
253InBlock.gif                            conn.Dispose();
254InBlock.gif                            throw ex;
255ExpandedSubBlockEnd.gif                        }

256ExpandedSubBlockEnd.gif                    }

257ExpandedSubBlockEnd.gif                }

258ExpandedSubBlockEnd.gif            }

259ExpandedSubBlockEnd.gif        }

260InBlock.gif
261ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
262InBlock.gif        /// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
263InBlock.gif        /// </summary>
264InBlock.gif        /// <param name="SQLString">计算查询结果语句</param>
265ExpandedSubBlockEnd.gif        /// <returns>查询结果(object)</returns>

266InBlock.gif        public object GetSingle(string SQLString, DbParameter[] cmdParms)
267ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
268InBlock.gif            using (DbConnection connection = provider.CreateConnection())
269ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
270InBlock.gif                connection.ConnectionString = connectionString;
271InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
272ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
273InBlock.gif                    try
274ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
275InBlock.gif                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
276InBlock.gif                        object obj = cmd.ExecuteScalar();
277InBlock.gif                        cmd.Parameters.Clear();
278InBlock.gif                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
279ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
280InBlock.gif                            return null;
281ExpandedSubBlockEnd.gif                        }

282InBlock.gif                        else
283ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
284InBlock.gif                            return obj;
285ExpandedSubBlockEnd.gif                        }

286ExpandedSubBlockEnd.gif                    }

287InBlock.gif                    catch (DbException e)
288ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
289InBlock.gif                        connection.Close();
290InBlock.gif                        connection.Dispose();
291InBlock.gif                        throw new Exception(e.Message);
292ExpandedSubBlockEnd.gif                    }

293ExpandedSubBlockEnd.gif                }

294ExpandedSubBlockEnd.gif            }

295ExpandedSubBlockEnd.gif        }

296InBlock.gif
297ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
298InBlock.gif        /// 执行查询语句,返回SqlDataReader
299InBlock.gif        /// </summary>
300InBlock.gif        /// <param name="strSQL">查询语句</param>
301ExpandedSubBlockEnd.gif        /// <returns>SqlDataReader</returns>

302InBlock.gif        public DbDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
303ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
304InBlock.gif            DbConnection connection = provider.CreateConnection();
305InBlock.gif            connection.ConnectionString = connectionString;
306InBlock.gif            DbCommand cmd = provider.CreateCommand();
307InBlock.gif            try
308ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
309InBlock.gif                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
310InBlock.gif                DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
311InBlock.gif                cmd.Parameters.Clear();
312InBlock.gif                return myReader;
313ExpandedSubBlockEnd.gif            }

314InBlock.gif            catch (DbException e)
315ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
316InBlock.gif                connection.Close();
317InBlock.gif                connection.Dispose();
318InBlock.gif                throw new Exception(e.Message);
319ExpandedSubBlockEnd.gif            }

320InBlock.gif
321ExpandedSubBlockEnd.gif        }

322InBlock.gif
323ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
324InBlock.gif        /// 执行查询语句,返回DataSet
325InBlock.gif        /// </summary>
326InBlock.gif        /// <param name="SQLString">查询语句</param>
327ExpandedSubBlockEnd.gif        /// <returns>DataSet</returns>

328InBlock.gif        public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
329ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
330InBlock.gif            using (DbConnection connection = provider.CreateConnection())
331ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
332InBlock.gif                using (DbCommand cmd = provider.CreateCommand())
333ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
334InBlock.gif                    using (DbDataAdapter da = provider.CreateDataAdapter())
335ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
336InBlock.gif                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
337InBlock.gif                        da.SelectCommand = cmd;
338InBlock.gif                        DataSet ds = new DataSet();
339InBlock.gif                        try
340ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
341InBlock.gif                            da.Fill(ds, "ds");
342InBlock.gif                            cmd.Parameters.Clear();
343InBlock.gif                            return ds;
344ExpandedSubBlockEnd.gif                        }

345InBlock.gif                        catch (DbException ex)
346ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
347InBlock.gif                            connection.Close();
348InBlock.gif                            connection.Dispose();
349InBlock.gif                            throw new Exception(ex.Message);
350ExpandedSubBlockEnd.gif                        }

351ExpandedSubBlockEnd.gif                    }

352ExpandedSubBlockEnd.gif                }

353ExpandedSubBlockEnd.gif            }

354ExpandedSubBlockEnd.gif        }

355InBlock.gif
356InBlock.gif        private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, string cmdText, DbParameter[] cmdParms)
357ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
358InBlock.gif            if (conn.State != ConnectionState.Open)
359ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
360InBlock.gif                conn.Open();
361ExpandedSubBlockEnd.gif            }

362InBlock.gif            cmd.Connection = conn;
363InBlock.gif            cmd.CommandText = cmdText;
364InBlock.gif            if (trans != null)
365ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
366InBlock.gif                cmd.Transaction = trans;
367ExpandedSubBlockEnd.gif            }

368InBlock.gif            cmd.CommandType = CommandType.Text;//cmdType;
369InBlock.gif            if (cmdParms != null)
370ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
371InBlock.gif                foreach (DbParameter parm in cmdParms)
372ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
373InBlock.gif                    cmd.Parameters.Add(parm);
374ExpandedSubBlockEnd.gif                }

375ExpandedSubBlockEnd.gif            }

376ExpandedSubBlockEnd.gif        }

377InBlock.gif
378ExpandedSubBlockEnd.gif        #endregion

379InBlock.gif
380ContractedSubBlock.gifExpandedSubBlockStart.gif        存储过程操作#region 存储过程操作
381ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
382InBlock.gif        /// 执行存储过程;
383InBlock.gif        /// </summary>
384InBlock.gif        /// <param name="storeProcName">存储过程名</param>
385InBlock.gif        /// <param name="parameters">所需要的参数</param>
386ExpandedSubBlockEnd.gif        /// <returns>返回受影响的行数</returns>

387InBlock.gif        public int RunProcedureExecuteSql(string storeProcName, DbParameter[] parameters)
388ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
389InBlock.gif            using (DbConnection connection = provider.CreateConnection())
390ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
391InBlock.gif                connection.ConnectionString = connectionString;
392InBlock.gif                DbCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
393InBlock.gif                int rows = cmd.ExecuteNonQuery();
394InBlock.gif                cmd.Parameters.Clear();
395InBlock.gif                connection.Close();
396InBlock.gif                return rows;
397ExpandedSubBlockEnd.gif            }

398ExpandedSubBlockEnd.gif        }

399InBlock.gif
400ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
401InBlock.gif        /// 执行存储过程,返回首行首列的值
402InBlock.gif        /// </summary>
403InBlock.gif        /// <param name="storeProcName">存储过程名</param>
404InBlock.gif        /// <param name="parameters">存储过程参数</param>
405ExpandedSubBlockEnd.gif        /// <returns>返回首行首列的值</returns>

406InBlock.gif        public Object RunProcedureGetSingle(string storeProcName, DbParameter[] parameters)
407ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
408InBlock.gif            using (DbConnection connection = provider.CreateConnection())
409ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
410InBlock.gif                connection.ConnectionString = connectionString;
411InBlock.gif                try
412ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
413InBlock.gif                    DbCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
414InBlock.gif                    object obj = cmd.ExecuteScalar();
415InBlock.gif                    cmd.Parameters.Clear();
416InBlock.gif                    if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
417ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
418InBlock.gif                        return null;
419ExpandedSubBlockEnd.gif                    }

420InBlock.gif                    else
421ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
422InBlock.gif                        return obj;
423ExpandedSubBlockEnd.gif                    }

424ExpandedSubBlockEnd.gif                }

425InBlock.gif                catch (DbException e)
426ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
427InBlock.gif                    connection.Close();
428InBlock.gif                    connection.Dispose();
429InBlock.gif                    throw new Exception(e.Message);
430ExpandedSubBlockEnd.gif                }

431ExpandedSubBlockEnd.gif            }

432ExpandedSubBlockEnd.gif        }

433InBlock.gif
434ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
435InBlock.gif        /// 执行存储过程
436InBlock.gif        /// </summary>
437InBlock.gif        /// <param name="storedProcName">存储过程名</param>
438InBlock.gif        /// <param name="parameters">存储过程参数</param>
439ExpandedSubBlockEnd.gif        /// <returns>SqlDataReader</returns>

440InBlock.gif        public DbDataReader RunProcedureGetDataReader(string storedProcName, DbParameter[] parameters)
441ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
442InBlock.gif            DbConnection connection = provider.CreateConnection();
443InBlock.gif            connection.ConnectionString = connectionString;
444InBlock.gif            DbDataReader returnReader;
445InBlock.gif            DbCommand cmd = BuildQueryCommand(connection, storedProcName, parameters);
446InBlock.gif            cmd.CommandType = CommandType.StoredProcedure;
447InBlock.gif            returnReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
448InBlock.gif            cmd.Parameters.Clear();
449InBlock.gif            return returnReader;
450ExpandedSubBlockEnd.gif        }

451InBlock.gif
452ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
453InBlock.gif        /// 执行存储过程
454InBlock.gif        /// </summary>
455InBlock.gif        /// <param name="storedProcName">存储过程名</param>
456InBlock.gif        /// <param name="parameters">存储过程参数</param>
457ExpandedSubBlockEnd.gif        /// <returns>DataSet</returns>

458InBlock.gif        public DataSet RunProcedureGetDataSet(string storedProcName, DbParameter[] parameters)
459ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
460InBlock.gif            using (DbConnection connection = provider.CreateConnection())
461ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
462InBlock.gif                connection.ConnectionString = connectionString;
463InBlock.gif                DataSet dataSet = new DataSet();
464InBlock.gif                DbDataAdapter sqlDA = provider.CreateDataAdapter();
465InBlock.gif                sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
466InBlock.gif                sqlDA.Fill(dataSet);
467InBlock.gif                sqlDA.SelectCommand.Parameters.Clear();
468InBlock.gif                sqlDA.Dispose();
469InBlock.gif                return dataSet;
470ExpandedSubBlockEnd.gif            }

471ExpandedSubBlockEnd.gif        }

472InBlock.gif
473ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
474InBlock.gif        /// 执行多个存储过程,实现数据库事务。
475InBlock.gif        /// </summary>
476ExpandedSubBlockEnd.gif        /// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>

477InBlock.gif        public bool RunProcedureTran(Hashtable SQLStringList)
478ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
479InBlock.gif            using (DbConnection connection = provider.CreateConnection())
480ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
481InBlock.gif                connection.ConnectionString = connectionString;
482InBlock.gif                connection.Open();
483InBlock.gif                using (DbTransaction trans = connection.BeginTransaction())
484ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
485InBlock.gif                    using (DbCommand cmd = provider.CreateCommand())
486ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
487InBlock.gif                        try
488ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
489InBlock.gif                            //循环
490InBlock.gif                            foreach (DictionaryEntry myDE in SQLStringList)
491ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
492InBlock.gif                                cmd.Connection = connection;
493InBlock.gif                                string storeName = myDE.Value.ToString();
494InBlock.gif                                DbParameter[] cmdParms = (DbParameter[])myDE.Key;
495InBlock.gif
496InBlock.gif                                cmd.Transaction = trans;
497InBlock.gif                                cmd.CommandText = storeName;
498InBlock.gif                                cmd.CommandType = CommandType.StoredProcedure;
499InBlock.gif                                if (cmdParms != null)
500ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
501InBlock.gif                                    foreach (DbParameter parameter in cmdParms)
502ExpandedSubBlockStart.gifContractedSubBlock.gif                                    dot.gif{
503InBlock.gif                                        cmd.Parameters.Add(parameter);
504ExpandedSubBlockEnd.gif                                    }

505ExpandedSubBlockEnd.gif                                }

506InBlock.gif                                int val = cmd.ExecuteNonQuery();
507InBlock.gif                                cmd.Parameters.Clear();
508ExpandedSubBlockEnd.gif                            }

509InBlock.gif                            trans.Commit();
510InBlock.gif                            return true;
511ExpandedSubBlockEnd.gif                        }

512InBlock.gif                        catch
513ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
514InBlock.gif                            trans.Rollback();
515InBlock.gif                            connection.Close();
516InBlock.gif                            connection.Dispose(); 
517InBlock.gif                            return false;
518ExpandedSubBlockEnd.gif                        }

519ExpandedSubBlockEnd.gif                    }

520ExpandedSubBlockEnd.gif                }

521ExpandedSubBlockEnd.gif            }

522ExpandedSubBlockEnd.gif        }

523InBlock.gif
524ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
525InBlock.gif        /// 执行多个存储过程,实现数据库事务。
526InBlock.gif        /// </summary>
527ExpandedSubBlockEnd.gif        /// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>

528InBlock.gif        public bool RunProcedureTran(C_HashTable SQLStringList)
529ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
530InBlock.gif            using (DbConnection connection = provider.CreateConnection())
531ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
532InBlock.gif                connection.ConnectionString = connectionString;
533InBlock.gif                connection.Open();
534InBlock.gif                using (DbTransaction trans = connection.BeginTransaction())
535ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
536InBlock.gif                    using (DbCommand cmd = provider.CreateCommand())
537ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
538InBlock.gif                        try
539ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
540InBlock.gif                            //循环
541InBlock.gif                            foreach (DbParameter[] cmdParms in SQLStringList.Keys)
542ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
543InBlock.gif                                cmd.Connection = connection;
544InBlock.gif                                string storeName = SQLStringList[cmdParms].ToString();
545InBlock.gif
546InBlock.gif                                cmd.Transaction = trans;
547InBlock.gif                                cmd.CommandText = storeName;
548InBlock.gif                                cmd.CommandType = CommandType.StoredProcedure;
549InBlock.gif                                if (cmdParms != null)
550ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
551InBlock.gif                                    foreach (DbParameter parameter in cmdParms)
552ExpandedSubBlockStart.gifContractedSubBlock.gif                                    dot.gif{
553InBlock.gif                                        cmd.Parameters.Add(parameter);
554ExpandedSubBlockEnd.gif                                    }

555ExpandedSubBlockEnd.gif                                }

556InBlock.gif                                int val = cmd.ExecuteNonQuery();
557InBlock.gif                                cmd.Parameters.Clear();
558ExpandedSubBlockEnd.gif                            }

559InBlock.gif                            trans.Commit();
560InBlock.gif                            return true;
561ExpandedSubBlockEnd.gif                        }

562InBlock.gif                        catch
563ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
564InBlock.gif                            trans.Rollback();
565InBlock.gif                            connection.Close();
566InBlock.gif                            connection.Dispose();
567InBlock.gif                            return false;                            
568ExpandedSubBlockEnd.gif                        }

569ExpandedSubBlockEnd.gif                    }

570ExpandedSubBlockEnd.gif                }

571ExpandedSubBlockEnd.gif            }

572ExpandedSubBlockEnd.gif        }

573InBlock.gif
574ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
575InBlock.gif        /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
576InBlock.gif        /// </summary>
577InBlock.gif        /// <param name="connection">数据库连接</param>
578InBlock.gif        /// <param name="storedProcName">存储过程名</param>
579InBlock.gif        /// <param name="parameters">存储过程参数</param>
580ExpandedSubBlockEnd.gif        /// <returns>SqlCommand</returns>

581InBlock.gif        private DbCommand BuildQueryCommand(DbConnection connection, string storedProcName, DbParameter[] parameters)
582ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
583InBlock.gif            if (connection.State != ConnectionState.Open)
584ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
585InBlock.gif                connection.Open();
586ExpandedSubBlockEnd.gif            }

587InBlock.gif            DbCommand command = provider.CreateCommand();
588InBlock.gif            command.CommandText = storedProcName;
589InBlock.gif            command.Connection = connection;
590InBlock.gif            command.CommandType = CommandType.StoredProcedure;
591InBlock.gif            if (parameters != null)
592ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
593InBlock.gif                foreach (DbParameter parameter in parameters)
594ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
595InBlock.gif                    command.Parameters.Add(parameter);
596ExpandedSubBlockEnd.gif                }

597ExpandedSubBlockEnd.gif            }

598InBlock.gif            return command;
599ExpandedSubBlockEnd.gif        }

600ExpandedSubBlockEnd.gif        #endregion

601ExpandedSubBlockEnd.gif    }

602ExpandedBlockEnd.gif}

603None.gif

代码下载

转载于:https://www.cnblogs.com/houleixx/archive/2009/02/09/SQLOperator.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值