ADO. NET and parameterized stored procedures

Source: Internet
Author: User

Using ADO. Net with parameterized stored procedures

 

ADO. net class that deals with input and output stored procedure parameters is dbcommand. this shouldn't come as a big surprise-dbcommand is responsible for executing commands on the database, so it makes sense that it shoshould also deal with their parameters. (Remember That dbcommand is just a base class for "real" command objects, such as sqlcommand .)

Using input parameters

 

When adding an input parameter to a command object, you need to specify the parameter's name, data type, and value. the dbcommand object stores its parameters in a collection named parameters, which contains dbparameter objects. each dbparameter instance represents a parameter.

Given that you have a dbcommand object named comm, the following code snippet creates a dbparameter object for the command using the createparameter method, sets its properties, and adds the parameter to the Command's Parameters collection.

// Create a new parameter
Dbparameter Param = comm. createparameter ();
Param. parametername = "@ departmentid ";
Param. value = value;
Param. dbtype = dbtype. int32;
Comm. Parameters. Add (PARAM );

The Command's createparameter method always returns a parameter object type specific to the data provider you're using, so the dbparameter object will be actually reference A sqlparameter instance if you're using SQL Server, and so on.

Another important property of dbparameter is size, which is good to set for data types that don't have fixed values, such as varchar. for numerical columns, specify the parameter size in bytes. for columns that store strings (such as char, varchar, or even text), specify the size in number of characters. longer strings are automatically truncated to the size specified for the parameter.

 

Using output parameters

 

Output stored procedure parameters behave like out parameters in C #. they are much like return values, in that you set their value in the stored procedure and read it from the calling function after executing the procedure. output parameters are especially useful when you have more return values, when you want to return non-integer data, or when you prefer to keep using the return value for indicating executing success (or for some other purpose ).
The code that creates an output parameter is as follows:

// Create a new parameter
Param = comm. createparameter ();
Param. parametername = "@ howmanyproducts ";
Param. Direction = parameterdirection. output;
Param. dbtype = dbtype. int32;
Comm. Parameters. Add (PARAM );

This is almost the same as the code for the input parameter, parameter t instead of supplying a value for the parameter, you set its direction property to parameterdirection. output. this tells the command that @ howmanyproducts is an output parameter.

 

Not original, copied from ,:Beginning ASP. Net e-commerce in C # from novice to professional

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.