C # Call the stored procedure with output parameters,

Source: Internet
Author: User

C # Call the stored procedure with output parameters,

Write a stored procedure in SQL server:

  1. Create procedure ProGetPWD
  2. @ Username varchar (20 ),
  3. @ Password varchar (20) OUTPUT
  4. AS
  5. BEGIN
  6. SELECT @ password = password
  7. FROM Users
  8. WHERE username = @ username
  9. END

--------------------------

The following describes how to call a stored procedure in. NET:

  1. String strConnection = "user id = sa; password = sa; initial catalog = MyTest; Server = YHB; Connect Timeout = 30 ";
  2. Using (SqlConnection conn = new SqlConnection (strConnection ))
  3. {
  4. Conn. Open ();
  5. Using (SqlCommand sqlComm = conn. CreateCommand ())
  6. {
  7. // Set the name of the stored procedure to be called
  8. SqlComm. CommandText = "GetPWD ";
  9. // Specify the name of the stored procedure passed to the database by the SqlCommand object instead of the SQL statement.
  10. SqlComm. CommandType = CommandType. StoredProcedure;
  11. SqlParameter username = sqlComm. Parameters. Add (new SqlParameter ("@ username", SqlDbType. VarChar, 20 ));
  12. // Specify "@ username" as the input parameter
  13. Username. Direction = ParameterDirection. Input;
  14. // Assign a value to the "@ username" parameter
  15. Username. Value = this.txt _ username. Text;
  16. SqlParameter password = sqlComm. Parameters. Add (new SqlParameter ("@ password", SqlDbType. VarChar, 20 ));
  17. // Specify "@ password" as the output parameter
  18. Password. Direction = ParameterDirection. Output;
  19. // Execute
  20. SqlComm. ExecuteNonQuery ();
  21. // Obtain the value of the output parameter and assign the value to the name. Note that the value is of the object type, and the type rotation is required.
  22. String passwrod = Convert. ToString (sqlComm. Parameters ["@ password"]. Value );
  23. MessageBox. Show (passwrod );
  24. }
  25. }

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.