logo资料库

sqlhelper c#操作数据库,添加删除修改,存储过程.doc

第1页 / 共31页
第2页 / 共31页
第3页 / 共31页
第4页 / 共31页
第5页 / 共31页
第6页 / 共31页
第7页 / 共31页
第8页 / 共31页
资料共31页,剩余部分请下载后查看
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.ComponentModel; namespace SQLHelper { /// /// SQLHelper 类封装对 SQL Server 数据库的添加、删除、修改和选择等操作 /// public class SQLHelper { /// 连接数据源 private SqlConnection myConnection = null; private readonly string RETURNVALUE = "RETURNVALUE"; /// /// 打开数据库连接. /// private void Open()
{ // 打开数据库连接 if (myConnection == null) { myConnection = new SqlConnection(ConfigurationManager.AppSetting s["SQLCONNECTIONSTRING"].ToString()); } if(myConnection.State == ConnectionState.Closed) { try { } ///打开数据库连接 myConnection.Open(); catch(Exception ex) { } SystemError.CreateErrorLog(ex.Message); finally { ///关闭已经打开的数据库连接
} } } /// /// 关闭数据库连接 /// public void Close() { ///判断连接是否已经创建 if(myConnection != null) { ///判断连接的状态是否打开 if(myConnection.State == ConnectionState.Open) myConnection.Close(); { } } } ///
/// 释放资源 /// public void Dispose() { } // 确认连接是否已经关闭 if (myConnection != null) { } myConnection.Dispose(); myConnection = null; /// /// 执行存储过程 /// /// 存储过程的名称 /// 返回存储过程返回值 public int RunProc(string procName) { SqlCommand cmd = CreateProcCommand(procName, null); try
{ } ///执行存储过程 cmd.ExecuteNonQuery(); catch(Exception ex) { } ///记录错误日志 SystemError.CreateErrorLog(ex.Message); finally { } ///关闭数据库的连接 Close(); ///返回存储过程的参数值 return (int)cmd.Parameters[RETURNVALUE].Value; } /// /// 执行存储过程
/// /// 存储过程名称 /// 存储过程所需参数 /// 返回存储过程返回值 public int RunProc(string procName, SqlParameter[] prams) { SqlCommand cmd = CreateProcCommand(procName, prams); try { } ///执行存储过程 cmd.ExecuteNonQuery(); catch(Exception ex) { } ///记录错误日志 SystemError.CreateErrorLog(ex.Message); finally { ///关闭数据库的连接 Close();
} ///返回存储过程的参数值 return (int)cmd.Parameters[RETURNVALUE].Value; } /// /// 执行存储过程 /// /// 存储过程的名称 /// 返回存储过程返回值 public void RunProc(string procName, out SqlDataReader dataReader) { ///创建 Command SqlCommand cmd = CreateProcCommand(procName, null); try { ///读取数据 dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnectio n);
} catch(Exception ex) dataReader = null; ///记录错误日志 SystemError.CreateErrorLog(ex.Message); { } } /// /// 执行存储过程 /// /// 存储过程的名称 /// 存储过程所需参数 /// 返回 DataReader 对象 public void RunProc(string procName, SqlParameter[] prams, out SqlDataRe ader dataReader) { ///创建 Command SqlCommand cmd = CreateProcCommand(procName, prams);
分享到:
收藏