Use of SQL Stored procedure instances in. net

Source: Internet
Author: User

◆ Standard component programming is allowed for stored procedures

◆ Fast execution of stored procedures

◆ Stored procedures can reduce network traffic

◆ Stored procedures can be fully utilized as a security mechanism

The author of this article will introduce it to you. NET database application stored procedures, and how to associate it with ADO.. NET SqlDataAdapter object, DataSet object, and so on. the overall performance of the. NET database application.

Create a simple stored procedure

1. Stored Procedures for queries without parameters

The code is as follows: Copy code

Create proc [dbo]. [SelectUsers]
AS
Begin
SELECT * from dbo. FMS_Users
End

Run the following sentence to obtain the result:

Exec SelectUsers

After creating the stored procedure, save it. After saving, the node corresponding to the stored procedure appears in the server resource manager. Note that the CREATE keyword in the code editing window is changed to the ALTER keyword, which is used to change any existing stored procedure. To run the above stored procedure, just click its node and right-click the menu to execute the stored procedure ",
 

Well, we will call it in the IDE environment:

Write a method and call it in a proper place.

 

The code is as follows: Copy code

Private void Stroedprocedure ()
        {
String con = System. Configuration. ConfigurationManager. ConnectionStrings ["consql"]. ToString ();
SqlConnection conn = new SqlConnection (con );
Conn. Open ();
// Name of the stored procedure and database connection
SqlCommand cmd = new SqlCommand ("SelectUsers", conn );
// Cmd. CommandType = CommandType. StoredProcedure;
// Cmd. CommandText = "SelectUsers ";
// Cmd. Connection = conn;
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = cmd;
DataTable ds = new DataTable ();
Da. Fill (ds );
           
GridView1.DataSource = ds;
GridView1.DataBind ();
          

           
        }

OK has completed the instance call of the stored procedure without parameters!

Second, call the stored procedure with parameters (the same principle)

Create stored procedure statement

The code is as follows: Copy code
Create proc [dbo]. [SelectParUsers]
(
@ Id int
)
As
Begin
Select * from dbo. FMS_Users where userId = @ id
End

Run the following statement to obtain the result:

Exec SelectParUsers 22


Search for data by a specific ID

 

The code is as follows: Copy code

Private static DataTable Stroedprocedure (int id)
        {
String con = System. Configuration. ConfigurationManager. ConnectionStrings ["consql"]. ToString ();
SqlConnection conn = new SqlConnection (con );
Conn. Open ();
SqlCommand cmd = new SqlCommand ("SelectParUsers", conn );
Cmd. CommandType = CommandType. StoredProcedure;
// Cmd. CommandText = "SelectParUsers ";
// Cmd. Connection = conn;
SqlDataAdapter da = new SqlDataAdapter (cmd );
Cmd. Parameters. Add ("@ id", SqlDbType. Int, 32). Value = id;
// SqlDataAdapter da = new SqlDataAdapter (cmd );
// Da. SelectCommand = cmd;
DataTable ds = new DataTable ();
Da. Fill (ds );
           
Return ds

           
        }


OK, the basic data query stored procedure ends. Next, add, delete, and modify operations.

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.