转自别人的一个很有用的数据库连接类

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Configuration;
None.gif
using  System.ComponentModel; namespace  SQLHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SQLHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 连接数据源
InBlock.gif
        private SqlConnection myConnection;
InBlock.gif        
private readonly string RETURNVALUE = "RETURNVALUE";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 打开数据库连接.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void Open() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 打开数据库连接
InBlock.gif
            if (myConnection == null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myConnection 
= new SqlConnection(ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString());                
ExpandedSubBlockEnd.gif            }
                
InBlock.gif            
if(myConnection.State == ConnectionState.Closed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{   
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
/**////打开数据库连接
InBlock.gif                    myConnection.Open();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    SystemError.SystemLog(ex.Message);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
/**////关闭已经打开的数据库连接                
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 关闭数据库连接
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Close() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////判断连接是否已经创建
InBlock.gif            if(myConnection != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**////判断连接的状态是否打开
InBlock.gif                if(myConnection.State == ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myConnection.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 释放资源
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Dispose() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 确认连接是否已经关闭
InBlock.gif
            if (myConnection != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                myConnection.Dispose();
InBlock.gif                myConnection 
= null;
ExpandedSubBlockEnd.gif            }
                
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="procName">存储过程的名称</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回存储过程返回值</returns>

InBlock.gif        public int RunProc(string procName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand cmd 
= CreateCommand(procName, null);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**////执行存储过程
InBlock.gif                cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**////记录错误日志
InBlock.gif                SystemError.SystemLog(ex.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////关闭数据库的连接
InBlock.gif            Close();
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////返回存储过程的参数值
InBlock.gif            return (int)cmd.Parameters[RETURNVALUE].Value;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="procName">存储过程名称</param>
InBlock.gif        
/// <param name="prams">存储过程所需参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回存储过程返回值</returns>

InBlock.gif        public int RunProc(string procName, SqlParameter[] prams) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand cmd 
= CreateCommand(procName, prams);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**////执行存储过程
InBlock.gif                cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**////记录错误日志
InBlock.gif                SystemError.SystemLog(ex.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////关闭数据库的连接
InBlock.gif            Close();
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////返回存储过程的参数值
InBlock.gif            return (int)cmd.Parameters[RETURNVALUE].Value;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="procName">存储过程的名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="dataReader">返回存储过程返回值</param>

InBlock.gif        public void RunProc(string procName, out SqlDataReader dataReader) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////创建Command
InBlock.gif            SqlCommand cmd = CreateCommand(procName, null);
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////读取数据
InBlock.gif            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);                
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="procName">存储过程的名称</param>
InBlock.gif        
/// <param name="prams">存储过程所需参数</param>
ExpandedSubBlockEnd.gif        
/// <param name="dataReader">存储过程所需参数</param>

InBlock.gif        public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////创建Command
InBlock.gif            SqlCommand cmd = CreateCommand(procName, prams);
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////读取数据
InBlock.gif            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建一个SqlCommand对象以此来执行存储过程
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="procName">存储过程的名称</param>
InBlock.gif        
/// <param name="prams">存储过程所需参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回SqlCommand对象</returns>

InBlock.gif        private SqlCommand CreateCommand(string procName, SqlParameter[] prams) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////打开数据库连接
InBlock.gif            Open();
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////设置Command
InBlock.gif            SqlCommand cmd = new SqlCommand(procName, myConnection);
InBlock.gif            cmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////添加把存储过程的参数
InBlock.gif            if (prams != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (SqlParameter parameter in prams)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    cmd.Parameters.Add(parameter);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////添加返回参数ReturnValue
InBlock.gif            cmd.Parameters.Add(
InBlock.gif                
new SqlParameter(RETURNVALUE, SqlDbType.Int,4,ParameterDirection.ReturnValue,
InBlock.gif                
false,0,0,string.Empty, DataRowVersion.Default,null));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////返回创建的SqlCommand对象
InBlock.gif            return cmd;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 生成存储过程参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ParamName">存储过程名称</param>
InBlock.gif        
/// <param name="DbType">参数类型</param>
InBlock.gif        
/// <param name="Size">参数大小</param>
InBlock.gif        
/// <param name="Direction">参数方向</param>
InBlock.gif        
/// <param name="Value">参数值</param>
ExpandedSubBlockEnd.gif        
/// <returns>新的 parameter 对象</returns>

InBlock.gif        public SqlParameter CreateParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlParameter param;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////当参数大小为0时,不使用该参数大小值
InBlock.gif            if(Size > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                param 
= new SqlParameter(ParamName, DbType, Size);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**////当参数大小为0时,不使用该参数大小值
InBlock.gif                param = new SqlParameter(ParamName, DbType);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////创建输出类型的参数
InBlock.gif            param.Direction = Direction;
InBlock.gif            
if (!(Direction == ParameterDirection.Output && Value == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                param.Value 
= Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////返回创建的参数
InBlock.gif            return param;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 传入输入参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ParamName">存储过程名称</param>
InBlock.gif        
/// <param name="DbType">参数类型</param></param>
InBlock.gif        
/// <param name="Size">参数大小</param>
InBlock.gif        
/// <param name="Value">参数值</param>
ExpandedSubBlockEnd.gif        
/// <returns>新的parameter 对象</returns>

InBlock.gif        public SqlParameter CreateInParam(string ParamName, SqlDbType DbType, int Size, object Value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////创建输入类型的参数
InBlock.gif            return CreateParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
ExpandedSubBlockEnd.gif        }
        
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 传入返回值参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ParamName">存储过程名称</param>
InBlock.gif        
/// <param name="DbType">参数类型</param>
InBlock.gif        
/// <param name="Size">参数大小</param>
ExpandedSubBlockEnd.gif        
/// <returns>新的 parameter 对象</returns>

InBlock.gif        public SqlParameter CreateOutParam(string ParamName, SqlDbType DbType, int Size) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////创建输出类型的参数
InBlock.gif            return CreateParam(ParamName, DbType, Size, ParameterDirection.Output, null);
ExpandedSubBlockEnd.gif        }
        
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 传入返回值参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ParamName">存储过程名称</param>
InBlock.gif        
/// <param name="DbType">参数类型</param>
InBlock.gif        
/// <param name="Size">参数大小</param>
ExpandedSubBlockEnd.gif        
/// <returns>新的 parameter 对象</returns>

InBlock.gif        public SqlParameter CreateReturnParam(string ParamName, SqlDbType DbType, int Size) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**////创建返回类型的参数
InBlock.gif            return CreateParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, null);
ExpandedSubBlockEnd.gif        }
            
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/huiseguding/archive/2006/06/27/437300.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值