Asp.net implements the calling of Stored Procedure instances with output parameters, asp.net Stored Procedure

Source: Internet
Author: User

Asp.net implements the calling of Stored Procedure instances with output parameters, asp.net Stored Procedure

This example describes how to use asp.net to call a stored procedure with output parameters. We will share this with you for your reference. The details are as follows:

Foreground 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> the user name already exists. Please try another user name! </Font> ");} else {Response. Write (" <font color = black> the 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;  }}

The sqlmodel class must set the type and length of the output parameter. Otherwise, an error occurs.

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)]; // Add the returned 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 design your asp.net program.

Articles you may be interested in:
  • Implementation of calling stored procedure by IDataParameter in asp.net
  • A new solution for calling the Stored Procedure Method in ASP. NET
  • How to call SQL stored procedures in asp.net to implement Paging
  • How to call the oracle stored procedure in asp.net
  • Asp.net stored procedure call
  • Oracle stored procedure in asp.net (image and text)
  • Asp.net uses stored procedures to implement fuzzy search examples
  • Implementation Code for ASP. NET to obtain the returned values of Stored Procedures
  • Execute SQL statements using stored procedures in ASP. NET
  • Asp.net SQL stored procedure
  • Asp.net secure, practical, and simple Paging for large-capacity stored procedures
  • Asp.net implements the method of calling a stored procedure and returning values

Related Article

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.