当前位置:Gxlcms > asp.net > asp.net实现调用带有输出参数的存储过程实例

asp.net实现调用带有输出参数的存储过程实例

时间:2021-07-01 10:21:17 帮助过:12人阅读

本文实例讲述了asp.net实现调用带有输出参数的存储过程。分享给大家供大家参考,具体如下:

前台jqurey

<script type="text/javascript">
  $(document).ready(function(){
  $('#change_image').click(function(){ $('#imgAuthenCode').attr('src','CheckCode.aspx?'+Math.random());});
  $("#accounts").bind("blur",function(){
   $.ajax({
    url:"checkusername.aspx",
    type:"post",
    datatype:"html",
    data:{user_name:$("#accounts").val()},
    success:function(msg){$("#tip_accounts").html(msg);}
    });});
  });
</script>

aspx文件:

protected void Page_Load(object sender, EventArgs e)
{
  Entity.User us = new Entity.User();
  us.User_name = Request.Params["user_name"].ToString();
  if (us.User_CheckName())
  {
    Response.Write("<font color=red>用户名已经存在,请尝试其它用户名!</font>");
  }
  else
  {
    Response.Write("<font color=black>用户名可以使用!</font>");
  }
}

user类

public bool User_CheckName()
{
  try
  {
    string[,] sArray = new string[2, 2];
    sArray[0, 0] = "@user_name";
    sArray[1, 0] = "@r_id";
    sArray[0, 1] = User_name;
    sArray[1, 1] = null;
    Factory.SqlModel sm = new Factory.SqlModel();
    Id = sm.Proc_Return_R_ID("User_CheckName", sArray);
    if (Id > 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  catch (Exception e)
  {
    Log lg = new Log();
    lg.ExceptionError(e);
    return false;
  }
}

sqlmodel 类  一定要设置输出参数的类型 及长度  否则出现 错误

String[1]: the Size property has an invalid size of 0.

public int Proc_Return_R_ID(string proc_name, string[,] sArray)
{
  try
  {
    if (sArray.GetLength(0) >= 1)
    {
      DataBase db = new DataBase();
      SqlParameter[] sqlpar = new SqlParameter[sArray.GetLength(0)];//加入返回值
      for (int i = 0; i < sArray.GetLength(0); i++)
      {
        sqlpar[i] = new SqlParameter(sArray[i, 0], sArray[i, 1]);
      }
      sqlpar[sArray.GetLength(0) - 1].Direction = ParameterDirection.Output;
      sqlpar[sArray.GetLength(0) - 1].SqlDbType = SqlDbType.Int;
      return db.Proc_Return_R_ID(proc_name, sqlpar);
    }
    else
    {
      return 0;
    }
  }
  catch
  {
    return 0;
  }
}

DATABASE.cs 类

public int Proc_Return_R_ID(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();
      return (int)BaseSqlCommand.Parameters["@r_id"].Value;
    }
    else
    {
      return 0;
    }
  }
  catch
  {
    return 0;
  }
  finally
  {
    BaseSqlCommand.Parameters.Clear();
    CloseConnection();
  }
}

希望本文所述对大家asp.net程序设计有所帮助。

人气教程排行