首页 > 编程 > .NET > 正文

asp.net实现调用存储过程并带返回值的方法

2024-07-10 12:47:30
字体:
来源:转载
供稿:网友

本文实例讲述了asp.net实现调用存储过程并带返回值的方法。,具体如下:

/// <summary>/// DataBase 的摘要说明/// </summary>public class DataBase{    /// <summary>    ///DataBase 的摘要说明    /// </summary>    protected static SqlConnection BaseSqlConnection = new SqlConnection();//连接对象    protected SqlCommand BaseSqlCommand = new SqlCommand(); //命令对象    public DataBase()    {      //      // TODO: 在此处添加构造函数逻辑      //    }    protected void OpenConnection()    {      if (BaseSqlConnection.State == ConnectionState.Closed) //连接是否关闭        try        {          BaseSqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["productsunion"].ToString();          BaseSqlCommand.Connection = BaseSqlConnection;          BaseSqlConnection.Open();        }        catch (Exception ex)        {          throw new Exception(ex.Message);        }    }    public void CloseConnection()    {      if (BaseSqlConnection.State == ConnectionState.Open)      {        BaseSqlConnection.Close();        BaseSqlConnection.Dispose();        BaseSqlCommand.Dispose();      }    }    public bool Proc_Return_Int(string proc_name, params SqlParameter[] cmdParms)    {      try      {        OpenConnection();        if (cmdParms != null)        {          foreach (SqlParameter parameter in cmdParms)          {            if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&              (parameter.Value == null))            {              parameter.Value = DBNull.Value;            }            BaseSqlCommand.Parameters.Add(parameter);          }          BaseSqlCommand.CommandType = CommandType.StoredProcedure;          BaseSqlCommand.CommandText = proc_name;          BaseSqlCommand.ExecuteNonQuery();          if (BaseSqlCommand.Parameters["Return"].Value.ToString()== "0")          {            return true;          }          else          {            return false;          }        }        else        {          return false;        }      }      catch      {        return false;      }      finally      {        BaseSqlCommand.Parameters.Clear();        CloseConnection();      }    }}

加入了一个组合类

public class SqlModel:ISqlModel{  #region ISqlModel 成员  public bool Proc_Return_Int(string proc_name, string[,] sArray)  {    try    {      if (sArray.GetLength(0) >= 1)      {        DataBase db = new DataBase();        SqlParameter[] sqlpar = new SqlParameter[sArray.GetLength(0)+1];//加入返回值        for (int i = 0; i < sArray.GetLength(0); i++)        {          sqlpar[i] = new SqlParameter(sArray[i,0], sArray[i,1]);        }        sqlpar[sArray.GetLength(0)] = new SqlParameter("Return", SqlDbType.Int);        sqlpar[sArray.GetLength(0)].Direction = ParameterDirection.ReturnValue;        if (db.Proc_Return_Int(proc_name, sqlpar))        {          return true;        }        else        {          return false;        }      }      else      {        return false;      }    }    catch    {      return false;    }  }  #endregion}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表