SQLHelper.cs

// Copyright(C) 2000-2006 Shixin Corporation
None.gif
// All rights reserverd
None.gif
// 文件名: SQLHelper.cs
None.gif
// 创建者: 
None.gif
// 创建日期: 2006-03-21
None.gif
// 概述: DataAccess层的数据访问Helper文件,模仿自Microsoft DAAB1.0。
None.gif
// 修改人/修改日期:  
None.gif
// 开放STR_CONNECTION连接字符串,因为SqlConnection是非托管资源,无法自动回收。
None.gif

None.gif
using  System;
None.gif
using  System.Data;
None.gif
using  System.Xml;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Collections;
None.gif
using  System.Configuration;
None.gif
using  System.Collections.Specialized;
None.gif
None.gif
using  SPS.Common;
None.gif
None.gif
None.gif
namespace  SPS.DataAccess
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SQLHelper 被DataAccess里的任何类调用,执行Insert,Update,SetValid和Select等组成的存储过程
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SqlHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//缓存SqlParameter数组对象的Hashtable
InBlock.gif
        private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
InBlock.gif
InBlock.gif        
//连接字符串
InBlock.gif
        public static string STR_CONNECTION;
InBlock.gif
InBlock.gif        
//SqlConnection
InBlock.gif        
//private static SqlConnection conn; 
InBlock.gif

ContractedSubBlock.gifExpandedSubBlockStart.gif        
"Public Functions"#region "Public Functions"
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取SqlConnection
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remark>
InBlock.gif        
/// 因为SqlConnection不是托管资源,所以在这里无法回收,所以在这里屏蔽掉
InBlock.gif        
/// </remark>
ExpandedSubBlockEnd.gif        
/// <returns>SqlConnection对象</returns>

InBlock.gif//        public static SqlConnection GetConnection()
InBlock.gif
//        {
InBlock.gif
//            if(conn==null)
InBlock.gif
//                conn=new SqlConnection(STR_CONNECTION); 
InBlock.gif
//            return conn;
InBlock.gif
//        }
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行XXXInsert,XXXUpdate,XXXSetValid类型的存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 参数Hashtable里的Key名需要和存储过程里的参数名保持一致;
InBlock.gif        
/// 存储过程的参数全部要在Hashtable中存在;
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="hashtable">由存储过程参数组成的Hashtable</param>
InBlock.gif        
/// <param name="strSPName">存储过程名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>Insert情况的PKID、或Update、SetValid情况的改变记录数</returns>

InBlock.gif        public static int ModifyTable(Hashtable hashtable,string strSPName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//获取SQL连接
InBlock.gif            
//SqlConnection conn=GetConnection();
InBlock.gif
            using(SqlConnection conn=new SqlConnection(STR_CONNECTION))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//依据连接字符串和存储过程名称 构造出这个存储过程的参数数组
InBlock.gif
                SqlParameter[] sqlParas=GetSpParameterSet(conn.ConnectionString,strSPName);
InBlock.gif
InBlock.gif                
//循环位每个存储参数数组的元素赋值    
InBlock.gif
                for(int i=0,j=sqlParas.Length;i<j;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string strKeyName=sqlParas[i].ParameterName;
InBlock.gif                
InBlock.gif                    
//当没有输入参数
InBlock.gif
                    try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{sqlParas[i].Value=hashtable[strKeyName];}
InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{sqlParas[i].Value=null;}
InBlock.gif                    
InBlock.gif                    
//如果输入参数是null
InBlock.gif
                    if(sqlParas[i].Value==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
switch(sqlParas[i].SqlDbType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
case SqlDbType.Int:
InBlock.gif                                sqlParas[i].Value
=-1;
InBlock.gif                                
break;
InBlock.gif                            
case SqlDbType.Decimal:
InBlock.gif                                sqlParas[i].Value
=-1;
InBlock.gif                                
break;
InBlock.gif                            
default:
InBlock.gif                                sqlParas[i].Value
=DBNull.Value;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                
//执行存储过程
InBlock.gif
                SqlHelper.ExecuteNonQuery(conn,CommandType.StoredProcedure,strSPName,sqlParas);
InBlock.gif
InBlock.gif                
//取出输出参数的值,
InBlock.gif                
//注意: Insert,Update,SetValid存储过程,只允许第一个参数类型为 out
InBlock.gif
                return (int)sqlParas[0].Value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行GetXXX类型的存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 参数Hashtable里的Key名需要和存储过程里的参数名保持一致;
InBlock.gif        
/// 存储过程的参数全部要在Hashtable中存在;
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="hashtable">由存储过程参数组成的Hashtable</param>
InBlock.gif        
/// <param name="strSPName">存储过程名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet</returns>

InBlock.gif        public static DataSet GetDataSet(Hashtable hashtable,string strSPName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//获取SQL连接
InBlock.gif            
//SqlConnection conn=GetConnection();
InBlock.gif

InBlock.gif            
using(SqlConnection conn=new SqlConnection(STR_CONNECTION))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//依据连接字符串和存储过程名称 构造出这个存储过程的参数数组
InBlock.gif
                SqlParameter[] sqlParas=GetSpParameterSet(conn.ConnectionString,strSPName);
InBlock.gif
InBlock.gif                
//循环位每个存储参数数组的元素赋值    
InBlock.gif
                for(int i=0,j=sqlParas.Length;i<j;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string strKeyName=sqlParas[i].ParameterName;
InBlock.gif
InBlock.gif                    
//当没有输入参数
InBlock.gif
                    try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{sqlParas[i].Value=hashtable[strKeyName];}
InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{sqlParas[i].Value=null;}
InBlock.gif                    
InBlock.gif                    
//如果输入参数是null
InBlock.gif
                    if(sqlParas[i].Value==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
switch(sqlParas[i].SqlDbType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
case SqlDbType.Int:
InBlock.gif                                sqlParas[i].Value
=-1;
InBlock.gif                                
break;
InBlock.gif                            
case SqlDbType.Decimal:
InBlock.gif                                sqlParas[i].Value
=-1;
InBlock.gif                                
break;
InBlock.gif                            
default:
InBlock.gif                                sqlParas[i].Value
=DBNull.Value;
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                
//执行存储过程
InBlock.gif
                DataSet dtReturn=SqlHelper.ExecuteDataset(conn,CommandType.StoredProcedure,strSPName,sqlParas);
InBlock.gif
InBlock.gif                
//返回结果集
InBlock.gif
                return dtReturn;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Private utility methods & constructors#region Private utility methods & constructors
InBlock.gif
InBlock.gif        
static SqlHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NameValueCollection nvc
=(NameValueCollection)ConfigurationSettings.GetConfig("Database");
InBlock.gif            STR_CONNECTION
="server="+ DESEncryptor.DesDecrypt(nvc["Server"]) 
InBlock.gif                
+";database="+ DESEncryptor.DesDecrypt(nvc["Database"]) +";uid="
InBlock.gif                
+ DESEncryptor.DesDecrypt(nvc["UID"]) 
InBlock.gif                
+";pwd="+ DESEncryptor.DesDecrypt(nvc["PWD"]) +"";
InBlock.gif
//            STR_CONNECTION="server=.;database=QL_SPS;uid=sa;pwd=sa";
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 把SqlParameter数组赋值给Command
InBlock.gif        
/// 
InBlock.gif        
/// This behavior will prevent default values from being used, but
InBlock.gif        
/// this will be the less common case than an intended pure output parameter (derived as InputOutput)
InBlock.gif        
/// where the user provided no input value.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="command">要添加参数的SqlCommand</param>
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">被添加的SqlParameter数组</param>

InBlock.gif        private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (SqlParameter p in commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//check for derived output value with no value assigned
InBlock.gif
                if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    p.Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                command.Parameters.Add(p);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 按需创建SqlCommand,并且设定Connection,Transaction,命令类别
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="command">要创建的SqlCommand</param>
InBlock.gif        
/// <param name="connection">SQL Server连接</param>
InBlock.gif        
/// <param name="transaction">事务或null值</param>
InBlock.gif        
/// <param name="commandType">命令类别 (stored procedure)</param>
InBlock.gif        
/// <param name="commandText">存储过程名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">与SqlCommand有关的参数或null值</param>

InBlock.gif        private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//如果连接没有打开,则打开连接
InBlock.gif
            if (connection.State != ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                connection.Open();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//把数据库连接与SqlCommand关联起来
InBlock.gif
            command.Connection = connection;
InBlock.gif
InBlock.gif            
//设置存储过程名称
InBlock.gif
            command.CommandText = commandText;
InBlock.gif
InBlock.gif            
//如果需要Transaction,则设置Transaction
InBlock.gif
            if (transaction != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                command.Transaction 
= transaction;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//设置命令类型
InBlock.gif
            command.CommandType = commandType;
InBlock.gif
InBlock.gif            
//添加参数
InBlock.gif
            if (commandParameters != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                AttachParameters(command, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  执行一个不返回结果集的存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">SQL server数据库连接</param>
InBlock.gif        
/// <param name="commandType">命令类型 (stored procedure)</param>
InBlock.gif        
/// <param name="commandText">存储过程名</param>
InBlock.gif        
/// <param name="commandParameters">参数数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回命令所影响的记录数</returns>

InBlock.gif        private static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
//创建一个SqlCommand
InBlock.gif
            SqlCommand cmd = new SqlCommand();
InBlock.gif            PrepareCommand(cmd, connection, (SqlTransaction)
null, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//finally, execute the command.
InBlock.gif
            int retval = cmd.ExecuteNonQuery();
InBlock.gif    
InBlock.gif            
// detach the SqlParameters from the command object, so they can be used again.
InBlock.gif
            cmd.Parameters.Clear();
InBlock.gif            
return retval;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行一个带参数的存储过程,返回一个结果集
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">SQL Server连接</param>
InBlock.gif        
/// <param name="commandType">命令类别(stored procedure)</param>
InBlock.gif        
/// <param name="commandText">存储过程名称</param>
InBlock.gif        
/// <param name="commandParameters">参数数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回的DataSet</returns>

InBlock.gif        private static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//创建一个命令
InBlock.gif
            SqlCommand cmd = new SqlCommand();
InBlock.gif            PrepareCommand(cmd, connection, (SqlTransaction)
null, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//创建 DataAdapter 和 DataSet
InBlock.gif
            SqlDataAdapter da = new SqlDataAdapter(cmd);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif
InBlock.gif            da.Fill(ds);        
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif            
InBlock.gif            
//返回结果集
InBlock.gif
            return ds;                        
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从存储过程里面,构造出SqlParameter数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connectionString">SQL Server连接字符串</param>
InBlock.gif        
/// <param name="spName">存储过程名称</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">标志是否把返回值加入SqlParameter数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>SqlParameter数组</returns>

InBlock.gif        private static SqlParameter[] DiscoverSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
using (SqlConnection cn = new SqlConnection(connectionString)) 
InBlock.gif            
using (SqlCommand cmd = new SqlCommand(spName,cn))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cn.Open();
InBlock.gif                cmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif                
//要返回的数组从下面而来
InBlock.gif
                SqlCommandBuilder.DeriveParameters(cmd);
InBlock.gif
InBlock.gif                
if (!includeReturnValueParameter) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//默认SqlParameter数组的第一个元素是存储过程的返回值
InBlock.gif
                    cmd.Parameters.RemoveAt(0);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                SqlParameter[] discoveredParameters 
= new SqlParameter[cmd.Parameters.Count];
InBlock.gif
InBlock.gif                cmd.Parameters.CopyTo(discoveredParameters, 
0);
InBlock.gif
InBlock.gif                
return discoveredParameters;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//深拷贝缓存里的参数数组
InBlock.gif
        private static SqlParameter[] CloneParameters(SqlParameter[] originalParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter[] clonedParameters 
= new SqlParameter[originalParameters.Length];
InBlock.gif            
for (int i = 0, j = originalParameters.Length; i < j; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                clonedParameters[i] 
= (SqlParameter)((ICloneable)originalParameters[i]).Clone();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return clonedParameters;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从存储过程解析出这个存储过程的参数的集合
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 首先从数据库查询, 然后缓存起来供以后调用
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">SQL server 的 Connection String</param>
InBlock.gif        
/// <param name="spName">存储过程名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>SqlParameters数组</returns>

InBlock.gif        public static SqlParameter[] GetSpParameterSet(string connectionString, string spName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetSpParameterSet(connectionString, spName, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从存储过程解析出这个存储过程的参数的集合
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// 首先从数据库查询, 然后缓存起来供以后调用
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">SQL server 的 Connection String</param>
InBlock.gif        
/// <param name="spName">存储过程名称</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">标志返回值是否放到返回的参数数组</param>
ExpandedSubBlockEnd.gif        
/// <returns>SqlParameters数组</returns>

InBlock.gif        private static SqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//定义Key
InBlock.gif
            string hashKey = connectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter":"");
InBlock.gif
InBlock.gif            SqlParameter[] cachedParameters;
InBlock.gif            
InBlock.gif            
//依据Key从缓存Hashtable里取出值
InBlock.gif
            cachedParameters = (SqlParameter[])paramCache[hashKey];
InBlock.gif
InBlock.gif            
if (cachedParameters == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    //如果取出的值是null,则从数据库获取存储过程的所有参数,并且放入缓存        
InBlock.gif
                cachedParameters = (SqlParameter[])(paramCache[hashKey] = DiscoverSpParameterSet(connectionString, spName, includeReturnValueParameter));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
return CloneParameters(cachedParameters);
ExpandedSubBlockEnd.gif        }
    
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }
    
ExpandedBlockEnd.gif}

None.gif
项目中一直使用的一个类,觉得还是蛮好用的。

ExpandedBlockStart.gif ContractedBlock.gif public   abstract   class  SqlHelper  dot.gif {
InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public SqlHelper() dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 连接字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string CONN_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Environment.CurrentDirectory + @"\SalaryStat.mdb;User ID=admin;Password=;Jet OLEDB:Database Password=tcyald";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于执行一段SQL语句,只有三个最简的必要参数,省去了commandtype。
InBlock.gif        
/// To excute a SQL statement, which reuturns a integer stand for effect line number.
InBlock.gif        
/// default Command type is text
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串 (Conntection String)</param>
InBlock.gif        
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回影响行数 (effect line number)</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int ExecuteNonQuery(string connString, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
using (OleDbConnection conn = new OleDbConnection(connString)) dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, CommandType.Text, cmdText, cmdParms);
InBlock.gif    
InBlock.gif                
int val = cmd.ExecuteNonQuery();
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return val;
InBlock.gif    
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于执行一段SQL语句。
InBlock.gif        
/// To excute a SQL statement, which reuturns a integer stand for effect line number.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串 (Connection String)</param>
InBlock.gif        
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdText">command的类型,具体见:CommandType (Command type)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回影响行数 (effect line number)</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
using (OleDbConnection conn = new OleDbConnection(connString)) dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, cmdParms);
InBlock.gif                
int val = cmd.ExecuteNonQuery();
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return val;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于执行一段SQL语句。传入的值是connection.
InBlock.gif        
/// To excute a SQL statement, which reuturns a integer stand for effect line number.
InBlock.gif        
/// a connection is passed in instead of a connection string
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="conn">一个以初始化好的OleDbConnection (a Conncection)</param>
InBlock.gif        
/// <param name="connString">连接字符串 (Conntection String)</param>
InBlock.gif        
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回影响行数 (effect line number)</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int ExecuteNonQuery(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif
InBlock.gif            PrepareCommand(cmd, conn, 
null, cmdType, cmdText, cmdParms);
InBlock.gif            
int val = cmd.ExecuteNonQuery();
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif            
return val;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于执行一段SQL语句。需要传入一个事务Transaction.
InBlock.gif        
/// To excute a SQL statement, which reuturns a integer stand for effect line number.
InBlock.gif        
/// a transaction is reqired
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="trans">一个Trasaction (Trasaction)</param>
InBlock.gif        
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdText">command的类型,具体见:CommandType (Command type)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回影响行数 (effect line number)</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static int ExecuteNonQuery(OleDbTransaction trans, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
InBlock.gif            
int val = cmd.ExecuteNonQuery();
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif            
return val;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于执行一个Select语句返回一个datareader,省略了commandtype参数
InBlock.gif        
/// To excute a SQL statement, and reuturns a dataReader.
InBlock.gif        
/// default command type is text
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串 (Conntection String)</param>
InBlock.gif        
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>datareader</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static OleDbDataReader ExecuteReader(string connString, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(connString);
InBlock.gif
InBlock.gif            
// we use a try/catch here because if the method throws an exception we want to 
InBlock.gif            
// close the connection throw code, because no datareader will exist, hence the 
InBlock.gif            
// commandBehaviour.CloseConnection will not work
ExpandedSubBlockStart.gifContractedSubBlock.gif
            try dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, CommandType.Text, cmdText, cmdParms);
InBlock.gif                OleDbDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif                
//    cmd.Parameters.Clear();
InBlock.gif
                return rdr;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch dot.gif{
InBlock.gif                conn.Close();
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于执行一个Select语句返回一个datareader
InBlock.gif        
/// To excute a SQL statement, and reuturns a dataReader.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串 (Connection String)</param>
InBlock.gif        
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdText">command的类型,具体见:CommandType (Command type)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>dataReader</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static OleDbDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(connString);
InBlock.gif
InBlock.gif            
// we use a try/catch here because if the method throws an exception we want to 
InBlock.gif            
// close the connection throw code, because no datareader will exist, hence the 
InBlock.gif            
// commandBehaviour.CloseConnection will not work
ExpandedSubBlockStart.gifContractedSubBlock.gif
            try dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, cmdParms);
InBlock.gif                OleDbDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif                
//    cmd.Parameters.Clear();
InBlock.gif
                return rdr;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch dot.gif{
InBlock.gif                conn.Close();
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于读取一个值,查询所返回的是结果集中第一行的第一列,省去了commandtype
InBlock.gif        
/// To excute a SQL statement, and returns the first column of the first line
InBlock.gif        
/// Default command type is text
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串 (Conntection String)</param>
InBlock.gif        
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>the first column of the first line</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static object ExecuteScalar(string connString, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
using (OleDbConnection conn = new OleDbConnection(connString)) dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, CommandType.Text, cmdText, cmdParms);
InBlock.gif                
object val = cmd.ExecuteScalar();
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return val;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于读取一个值,查询所返回的是结果集中第一行的第一列
InBlock.gif        
/// To excute a SQL statement, and returns the first column of the first line
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串 (Connection String)</param>
InBlock.gif        
/// <param name="cmdType">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdText">command的类型,具体见:CommandType (Command type)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>the first column of the first line</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
using (OleDbConnection conn = new OleDbConnection(connString)) dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, cmdParms);
InBlock.gif                
object val = cmd.ExecuteScalar();
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return val;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于读取一个值,查询所返回的是结果集中第一行的第一列
InBlock.gif        
/// To excute a SQL statement, and returns the first column of the first line
InBlock.gif        
/// a connection is passed in instead of a connection string
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="conn">一个以初始化好的OleDbConnection (a Conncection)</param>
InBlock.gif        
/// <param name="connString">连接字符串 (Conntection String)</param>
InBlock.gif        
/// <param name="cmdText">command的字符串 (SQL Statement)</param>
InBlock.gif        
/// <param name="cmdParms">参数列表 (Paramters)</param>
ExpandedSubBlockEnd.gif        
/// <returns>the first column of the first line</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static object ExecuteScalar(OleDbConnection conn, CommandType cmdType, string cmdText, params OleDbParameter[] cmdParms) dot.gif{
InBlock.gif   
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif
InBlock.gif            PrepareCommand(cmd, conn, 
null, cmdType, cmdText, cmdParms);
InBlock.gif            
object val = cmd.ExecuteScalar();
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif            
return val;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在执行SQL语句之前的准备工作
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmd">command</param>
InBlock.gif        
/// <param name="conn">connection</param>
InBlock.gif        
/// <param name="trans">trasaction</param>
InBlock.gif        
/// <param name="cmdType">command类型</param>
InBlock.gif        
/// <param name="cmdText">command字符串</param>
ExpandedSubBlockEnd.gif        
/// <param name="cmdParms">参数列表</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, CommandType cmdType, string cmdText, OleDbParameter[] cmdParms) dot.gif{
InBlock.gif
InBlock.gif            
if (conn.State != ConnectionState.Open)
InBlock.gif                conn.Open();
InBlock.gif
InBlock.gif            cmd.Connection 
= conn;
InBlock.gif            cmd.CommandText 
= cmdText;
InBlock.gif
InBlock.gif            
if (trans != null)
InBlock.gif                cmd.Transaction 
= trans;
InBlock.gif
InBlock.gif            cmd.CommandType 
= cmdType;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (cmdParms != nulldot.gif{
InBlock.gif                
foreach (OleDbParameter parm in cmdParms)
InBlock.gif                    cmd.Parameters.Add(parm);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据SQL语句查询返回DataSet
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SQLString">查询的SQL语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static DataSet GetDataSet(string SQLString) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
using (OleDbConnection connection = new OleDbConnection(CONN_STRING)) dot.gif{
InBlock.gif                DataSet ds 
= new DataSet();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
try dot.gif{
InBlock.gif                    connection.Open();
InBlock.gif                    OleDbDataAdapter command 
= new OleDbDataAdapter(SQLString,connection);
InBlock.gif                    command.Fill(ds,
"ds");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
catch(System.Data.OleDb.OleDbException ex) dot.gif{
InBlock.gif                    
throw new Exception(ex.Message);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return ds;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif  
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据SQL语句和查询参数查询返回DataSet
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SQLString">查询的SQL语句</param>
InBlock.gif        
/// <param name="cmdParms">参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet</returns>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public static DataSet GetDataSet(string SQLString,params OleDbParameter[] cmdParms) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
using (OleDbConnection connection = new OleDbConnection(CONN_STRING)) dot.gif{
InBlock.gif                OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif                PrepareCommand(cmd, connection, 
null,CommandType.Text,SQLString, cmdParms);
ExpandedSubBlockStart.gifContractedSubBlock.gif                
using( OleDbDataAdapter da = new OleDbDataAdapter(cmd) ) dot.gif{
InBlock.gif                    DataSet ds 
= new DataSet();
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
try dot.gif{
InBlock.gif                        da.Fill(ds,
"ds");
InBlock.gif                        cmd.Parameters.Clear();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockStart.gifContractedSubBlock.gif                    
catch(System.Data.OleDb.OleDbException ex) dot.gif{
InBlock.gif                        
throw new Exception(ex.Message);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return ds;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/xiongeee/archive/2006/12/19/596876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值