Use the simplest and most effective code in C # To execute the stored procedure and return data

Source: Internet
Author: User

The stored procedure p_sys_Login is defined as follows:
Create procedure p_sys_Login
@ ArgUserID varchar (20), -- User Name
@ ArgPassword varchar (20), -- Password
@ ArgResult varchar (50) OUTPUT -- logon result
AS
/*
......
*/

The following shows how to use the most concise and effective code in C # To execute the stored procedure and return data:
/// <Summary>
/// User Logon Verification
/// </Summary>
/// <Param name = "userID"> User name </param>
/// <Param name = "password"> password </param>
Public void Login (string userID, string password)
{
// The database connection string is stored in Web. config.
String cnnString = ConfigurationSettings. etettings ["ConnectionString"];
SqlConnection cnn = new SqlConnection (cnnString );

//
String SQL = string. Format ("EXEC p_sys_Login '{0}', '{1}', @ Result OUTPUT ",
UserID, password );
SqlCommand cmd = new SqlCommand (SQL, cnn );

// Create and add parameters corresponding to "@ Result OUTPUT"
SqlParameter paramResult = new SqlParameter ("@ Result", SqlDbType. VarChar, 50 );
ParamResult. Direction = ParameterDirection. Output;
Cmd. Parameters. Add (paramResult );

Cnn. Open ();
Cmd. ExecuteNonQuery ();
Cnn. Close ();

// Obtain the results returned by the Stored Procedure
String result = paramResult. Value. ToString ();

//......
}

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.