asp.net implementation calls a stored procedure instance with an output parameter _ Practical tips

Source: Internet
Author: User

This article describes the ASP.net implementation invocation of stored procedures with output parameters. Share to everyone for your reference, specific as follows:

Front desk 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 file:

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> user name already exists, try another user name!") </font> ");
  }
  else
  {
    Response.Write ("<font color=black> user name can be used!") </font> ");
  }


User class

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 class Be sure to set the type and length of the output parameter otherwise an error occurs

STRING[1]: The Size property has a 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)];//join return value 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 class

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 ();
  }


I hope this article will help you to ASP.net program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.