The second training materials (SQL Server Stored Procedures and ADO. NET access stored procedures) of the Network Studio after the summer vacation (II)

Source: Internet
Author: User

Using ADO and NET to call stored procedures can be divided into four methods

1) call a stored procedure without Parameters

2) call a stored procedure with a parameter

3) call a stored procedure with multiple parameters

4) Call the stored procedure with output parameters

The stored procedure used in this sample code uses the stored procedure created by the author's previous blog, blog address: http://yisuowushinian.blog.51cto.com/4241271/1016524, please refer)

1. The stored procedure without parameters is called by ADO. NET. The stored procedure is the stored procedure created in 1-1 in the previous blog article.

The call code 1-1 is shown as follows:


 
 
  1. /// <Summary>

  2. /// Call a stored procedure without Parameters

  3. /// It is of little significance to call a stored procedure without parameters. Generally, queries are conditional queries.

  4. /// </Summary>

  5. Publicvoid ShowData1 ()

  6. {

  7. SqlConnection con = new SqlConnection (strConString );

  8. // Instantiate the SqlCommand object. The parameter object is specified as the name of the stored procedure to be accessed.

  9. SqlCommand cmd = new SqlCommand ("select_activity", con );

  10. // Specify the command type as stored procedure

  11. Cmd. CommandType = CommandType. StoredProcedure;

  12. // Create a dataset

  13. SqlDataAdapter adapter = new SqlDataAdapter (cmd );

  14. DataTable table = new DataTable ();

  15. Adapter. Fill (table );

  16. DataRow Dr = table. Rows [0];

  17. Response. Write (Dr [3]. ToString ());

  18. }

2. Because the previous blog does not create a stored procedure with only one parameter, the creation process is as follows:

 
 
  1. -- Create a stored procedure with a parameter

  2. Create proc select_activity3

  3. @ Id int

  4. As

  5. -- The SQL command executed by the Stored Procedure

  6. Select * from T_Activity where id = @ id;

  7. -- Call the select_activity Stored Procedure

  8. Exec select_activity3 5

Example 1-2 code for calling a stored procedure with a parameter in ADO. NET is as follows:

 
 
  1. /// <Summary>

  2. /// Call the stored procedure with a parameter

  3. /// </Summary>

  4. Publicvoid ShowData2 ()

  5. {

  6. SqlConnection con = new SqlConnection (strConString );

  7. SqlCommand cmd = new SqlCommand ("select_activity3", con );

  8. // Specify the command type as stored procedure

  9. Cmd. CommandType = CommandType. StoredProcedure;

  10. // Declare a parameter object

  11. SqlParameter parameter = new SqlParameter ();

  12. // Specify the parameter name

  13. Parameter. ParameterName = "@ id ";

  14. // Data type of the specified parameter

  15. Parameter. SqlDbType = SqlDbType. Int;

  16. // Assign a value to the parameter

  17. Parameter. Value = 5;

  18. // Add the command object to the parameter set of the command object

  19. Cmd. Parameters. Add (parameter );

  20. SqlDataAdapter adapter = new SqlDataAdapter (cmd );

  21. // Create a able object

  22. DataTable table = new DataTable ();

  23. Adapter. Fill (table );

  24. DataRow Dr = table. Rows [0];

  25. Response. Write (Dr [3]. ToString ());

  26. }

Iii. ADO.. NET calls a stored procedure with multiple parameters. In this example, the 1-3 Code uses two parameters as a demonstration. For more parameters, refer to this example, the stored procedure used is the example 1-2 in the previous blog


 
 
  1. /// <Summary>

  2. /// Call an array of parameters with two parameters

  3. /// </Summary>

  4. Publicvoid ShowData3 ()

  5. {

  6. SqlConnection con = new SqlConnection (strConString );

  7. SqlCommand cmd = new SqlCommand ("select_activity1", con );

  8. // Specify the command type as stored procedure

  9. Cmd. CommandType = CommandType. StoredProcedure;

  10. // Declare a multi-dimensional parameter Array

  11. SqlParameter [] parameter =

  12. {

  13. New SqlParameter ("@ id", SqlDbType. Int, 4 ),

  14. New SqlParameter ("@ activityName", SqlDbType. NVarChar, 50)

  15. };

  16. Parameter [0]. Value = 5;

  17. // Assign a value to the parameter

  18. Parameter [1]. Value = "";

  19. // Add the command object to the parameter set of the command object

  20. Cmd. Parameters. AddRange (parameter );

  21. SqlDataAdapter adapter = new SqlDataAdapter (cmd );

  22. // Create a able object

  23. DataTable table = new DataTable ();

  24. Adapter. Fill (table );

  25. DataRow Dr = table. Rows [0];

  26. Response. Write (Dr [3]. ToString ());

  27. }

4. ADO. NET access stored procedures with output parameters. The code 1-4 in this example uses one input parameter and one output parameter as a demonstration. For more parameters, refer to this example, the stored procedure used is an example of the previous blog article 1-4


 
 
  1. /// <Summary>

  2. /// Call a stored procedure with a returned value

  3. /// </Summary>

  4. Publicvoid ShowData4 ()

  5. {

  6. SqlConnection con = new SqlConnection (strConString );

  7. SqlCommand cmd = new SqlCommand ("select_MathResult", con );

  8. Cmd. CommandType = CommandType. StoredProcedure;

  9. SqlParameter [] parameter = {

  10. New SqlParameter ("@ activityName", SqlDbType. NVarChar, 50 ),

  11. New SqlParameter ("@ result", SqlDbType. Int, 4)

  12. };

  13. Parameter [0]. Value = "";

  14. // Set the output method direction of the output parameter

  15. Parameter [1]. Direction = ParameterDirection. Output;

  16. Cmd. Parameters. AddRange (parameter );

  17. SqlDataAdapter adapter = new SqlDataAdapter (cmd );

  18. DataTable table = new DataTable ();

  19. Adapter. Fill (table );

  20. Response. Write (Convert. ToInt32 (cmd. Parameters [1]. Value ));

  21. }

In this way, all examples of accessing SQL Server Stored Procedures by ADO. NET are completed. Because I found that there was too much content to complete in a blog, I could only divide it into several articles. Please keep an eye on my blog. We will update it for you next time, and learn about SQL Server table connection query, multi-Table query, paging, and so on.

Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held.

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1FG55393-0.png "/>

This article is from the blog of "Hadar column", please be sure to keep this source http://yisuowushinian.blog.51cto.com/4241271/1016527

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.