ADO. NET calls the stored procedure and ado.net Stored Procedure

Source: Internet
Author: User

ADO. NET calls the stored procedure and ado.net Stored Procedure

Create a table

CREATE TABLE [tab_cJ] ([id] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY,[name] [varchar] (50) ,[age] [int] NULL ,[info] [varchar] (200))

Create storage process

CREATE PROCEDURE QueryInfoByName@name varchar(50),@age intASselect info from tab_cj where [name]=@name and age=@ageGO

Call

Using (SqlConnection conn = new SqlConnection ("Data Source = .; initial Catalog = test; Integrated Security = True ") {SqlCommand cmd = conn. createCommand (); cmd. commandType = CommandType. storedProcedure; // specifies the execution of Stored Procedure operations cmd. commandText = "QueryInfoByName"; // stored procedure name // The first parameter of the corresponding Stored Procedure QueryInfoByName @ name SqlParameter parName = new SqlParameter ("@ name", SqlDbType. varChar, 50); // specify the value of @ name to be transferred to parName. value = "aa"; // The second parameter corresponding to the stored procedure QueryInfoByName @ age SqlParameter parAge = new SqlParameter ("@ age", SqlDbType. int); // specify the value of @ age to be transferred to parAge. value = 12; // this step is very important. You must add the two parameter types you have set to the cmd parameter set of the Command object. parameters. add (parName); cmd. parameters. add (parAge); // method 1. the query result must be displayed on a control such as DataGrid. DataSet ds = new DataSet (); SqlDataAdapter adapter = new SqlDataAdapter (cmd ); adapter. fill (ds); // method 2. Read the conn by a single value. open (); SqlDataReader reader = cmd. executeReader (); if (reader. hasRows) {while (reader. read () {Response. write (reader. getString (0 ));}}}

Call a stored procedure with output parameters

Create storage process

create procedure getAge(@name varchar(50),@age int output)asselect @age=age from tab_cJ where [name]=@name

Call the Stored Procedure

Using (SqlConnection conn = new SqlConnection ("Data Source = .; initial Catalog = test; Integrated Security = True ") {SqlCommand cmd = conn. createCommand (); cmd. commandType = CommandType. storedProcedure; // specifies the execution of Stored Procedure operations cmd. commandText = "getAge"; // name of the stored procedure SqlParameter parName = new SqlParameter ("@ name", SqlDbType. varChar, 50); parName. value = "aa"; // The output parameter @ age SqlParameter parAge = new SqlParameter ("@ age", SqlDbType. int); parAge. direction = ParameterDirection. output; cmd. parameters. add (parName); cmd. parameters. add (parAge); try {conn. open (); cmd. executeNonQuery (); int I = int. parse (parAge. value. toString (); Response. write (I. toString (); conn. close ();} catch (Exception ex) {Response. write (ex. toString ());}}

Extract ------

Chunhua qiushi

Such as intrusion and Deletion

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.