C # Call the stored procedure (with return parameters and no return parameters)

Source: Internet
Author: User

/// <Summary>
/// Stored procedure execution function
/// </Summary>
/// <Param name = "strspname"> stored procedure name </param>
/// <Param name = "ht"> parameter information set </param>
/// <Param name = "strparameterarray"> array of parameter names to be returned </param>
/// <Returns> hashtable with returned values </returns>
Public void execstoredprocedure (string strspname, hashtable HT, string [] strparameterarray)
{
Sqlcommand comm = new sqlcommand (strspname, Conn );
Comm. commandtype = commandtype. storedprocedure;

Idictionaryenumerator htenumerator = Ht. getenumerator ();
While (htenumerator. movenext ())
Comm. Parameters. Add (htenumerator. Key. tostring (), htenumerator. value );

Foreach (string strparametername in strparameterarray)
Comm. Parameters [strparametername]. Direction = parameterdirection. output;

Try
{
Openconn (); // open the database link
Comm. executenonquery ();
}
Finally
{
Closeconn (); // closes the database link
}

Ht. Clear ();
Foreach (string strparametername in strparameterarray)
Ht. Add (strparametername, Comm. Parameters [strparametername]. value );
}

/// <Summary>
/// Stored procedure execution function
/// </Summary>
/// <Param name = "strspname"> stored procedure name </param>
/// <Param name = "ht"> parameter information set </param>
Public void execstoredprocedure (string strspname, hashtable HT)
{
Sqlcommand comm = new sqlcommand (strspname, Conn );
Comm. commandtype = commandtype. storedprocedure;

Idictionaryenumerator htenumerator = Ht. getenumerator ();
While (htenumerator. movenext ())
Comm. Parameters. Add (htenumerator. Key. tostring (), htenumerator. value );

Try
{
Openconn (); // open the database link
Comm. executenonquery ();
}
Finally
{
Closeconn (); // closes the database link
}
}

Call method:

Hashtable ht = new hashtable ();
Ht. Add ("@ variable_1", "parameter 1 ");
Ht. Add ("@ variable_2", "parameter 2 ");
Ht. Add ("@ variable_3", "parameter 3 ");

// With returned data
String [] variablelist = {"@ variable_2", "@ variable_3 "};
Execstoredprocedure ("Stored Procedure name", hashtable HT, variablelist );
// Return data in the key corresponding to hashtable HT

// No returned data
Execstoredprocedure ("Stored Procedure name", hashtable HT );

// The execstoredprocedure method is a method that is reloaded twice.

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.