Use stored procedures in ADO. NET

Source: Internet
Author: User

It is said that the stored procedure is very simple to use, but the code to access it is quite tedious, but it is okay that you just need to clarify your ideas, this is not a problem, the simple Applet in the book explains how to create and call a stored procedure to create a stored procedure. The SQL statements of all columns of the ID returned are as follows: SELECT id, lname, fname, phone, address, city, state, zip, contract FROM authors WHERE id = whatever author ID we want create a stored procedure for the stored procedure transfer parameter. If the variable uses @ as the prefix, the SQL statement is as follows: SELECT id, lname, fname, phone, address, city, state, zip, contract FROM authors WHERE Id = @ id Note: A stored procedure can create an SQL statement, it can also be a set of complex statements. The T-SQL supports branch, loop, and other variable declarations, which will help compile some very complex Stored Procedure Code back to the example of the stored procedure knowledge line of SQL code, when we want to declare the Stored PROCEDURE for transmitting the parameter @ id and process name example, the SQL code is AS follows: CREATE PROCEDURE example @ id varchar (11) AS SELECT id, name, phone, address, city, state, zip, contract FROM authors WHERE Id = @ id and save the stored procedure in the database, you can access this stored procedure in the Code. To call the stored procedure, first create the stored procedure in the database. As mentioned above, the stored procedure is then created with the SqlCommand object, and initialize the SqlCommand object [html] <span style = "font-size: 18px;"> SqlCommand cmd = new SqlCommand (); cmd. C OmmandText = "example"; // specifies the stored procedure to be called. cmd. CommandType = CommandType. StoredProcedure; // specifies that the SQL command type is stored procedure. The default value is SQL statement. Cmd. connection = con; // set the Connection <span style = "background-color: white; font-family: Arial; line-height: 19pt; "> </span> Add the Stored Procedure Parameter [html SqlParameterparam = new SqlParameter (); // define a parameter object param to the SqlCommand object. parameterName = "@ id"; // name of the stored procedure parameter param. value = txtID. text. trim (); // the value of this parameter cmd. parameters. add (param); // SqlCommand object Add this parameter object param = new SqlParameter ("name", txtName. text. trim (); cmd. parameters. add (param); Use the qlCommand object to call the SQL Execution function. executeNonQuery (); ADO.. NET ......

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.