日期:2014-05-18 浏览次数:20520 次
using System.Data; using System.Data.SqlClient; using System.Configuration; using System.ComponentModel; namespace SQLHelper { /// <summary> /// SQLHelper类封装对SQL Server数据库的添加、删除、修改和选择等操作 /// </summary> public class SQLHelper { /// 连接数据源 private SqlConnection myConnection = null; private readonly string RETURNVALUE = "RETURNVALUE"; /// <summary> /// 打开数据库连接. /// </summary> private void Open() { // 打开数据库连接 if (myConnection == null) { myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["gzma03578ConnectionString"].ConnectionString); } if(myConnection.State == ConnectionState.Closed) { try { ///打开数据库连接 myConnection.Open(); } catch(Exception ex) { SystemError.CreateErrorLog(ex.Message); } finally { ///关闭已经打开的数据库连接 } } } /// <summary> /// 关闭数据库连接 /// </summary> public void Close() { ///判断连接是否已经创建 if(myConnection != null) { ///判断连接的状态是否打开 if(myConnection.State == ConnectionState.Open) { myConnection.Close(); } } } /// <summary> /// 释放资源 /// </summary> public void Dispose() { // 确认连接是否已经关闭 if (myConnection != null) { myConnection.Dispose(); myConnection = null; } } /// <summary> /// 执行存储过程 /// </summary> /// <param name="procName">存储过程的名称</param> /// <returns>返回存储过程返回值</returns> 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; }
------解决方案--------------------
/// <summary> /// 执行存储过程 /// </summary> /// <param name="procName">存储过程名称</param> /// <param name="prams">存储过程所需参数</param>