Calling stored procedures in C # is primarily a matter of parameter usage

Source: Internet
Author: User
Stored Procedures | issues

Calling stored procedures in C # is primarily a matter of parameter usage (input parameters, output parameters, return values)
It is assumed that Conn is a defined connection, proc is the stored procedure name

1, call no parameter no return of stored procedures
SqlCommand cmd=new SqlCommand (proc,conn);
Cmd.commandtype=commandtype.storedprocedure;//tells the execution of the stored procedure
Conn. Open ();
Cmd. ExecuteNonQuery ();

2, the use of input parameters and output parameters
Suppose there is a stored procedure
Create proc TestProc
//(
@test_in varchar (20),
@test_out varchar (OUTPUT)
//)

SqlCommand cmd=new SqlCommand (proc,conn);
Cmd.commandtype=commandtype.storedprocedure;//tells the execution of the stored procedure
SqlParameter sp=new SqlParameter ("@test_in", sqldbtype.varchar,20);
Sp. value= "Input Parameters";
Cmd. Parameters.Add (SP);//Add Parameters to command object
SqlParameter out=new SqlParameter ("@test_out", sqldbtype.varchar,20);
Out. direction=parameterdirection.output//Specifies that the parameter is an output parameter
Cmd. Parameters.Add (out);//Add Parameters to command object
Conn. Open ();
Cmd. ExecuteNonQuery ();
String Result=cmd. parameters["@test_out"]. Value.tostring ()///This is to get the returned output parameter value

3. Get return value
To get return values, you need to add returns parameters, such as receiving 1 returned values with a return type of int
SqlParameter returnparameter=new SqlParameter ("ReturnValue", sqldbtype.int,4);
RETURNPARAMETER.DIRECTION=PARAMETERDIRECTION.RETURNVALUE//Specifies the return value of this parameter

#endregion
Public Classdb ()
{

}
#region public bool Checklanding (string strlandname, String strlandpwd)



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.