How to call stored procedures in ASP. NET

Source: Internet
Author: User

ASP. NET and SQL Server are the best, slightly largerProgramGenerally, the first consideration is SQL Server, but access is used only when economic considerations exist. With SQL Server, stored procedures are generally used to improve database efficiency. Because stored procedures are fast to run and advanced query functions can be implemented. For example, some data parameters are input, but the SQL Execution process may be different.

The following is an example. To create a new role, the role name must be unique. The following is a stored procedure.

 
Create procedure sp_accountrole_create

@ Categoryid int,
@ Rolename nvarchar (10 ),
@ Description nvarchar (50 ),
@ Roleid int output
As
Declare @ count int

-- Query for records with the same name
Select @ COUNT = count (roleid) from account_role where
Rolename = @ rolename

If @ COUNT = 0

Insert into account_role
(Categoryid, rolename, description) Values
(@ Categoryid, @ rolename, @ description)

Set @ roleid = @ identity

Return 1
Go


C # process for executing the stored procedure:

Sqlconnection dbconnection = new sqlconnection (mconnectionstring );
Sqlcommand command = new sqlcommand ("sp_accountrole_create", dbconnection );
Dbconnection. Open (connectstring );
// Deprecated the sqlcommand attribute as the Stored Procedure
Command. commandtype = commandtype. storedprocedure;

Command. Parameters. Add ("@ categoryid", sqldbtype. Int, 4 );
Command. Parameters. Add ("@ rolename", sqldbtype. nvarchar, 10 );
Command. Parameters. Add ("@ description", sqldbtype. nvarchar, 50 );
Command. Parameters. Add ("@ roleid", sqldbtype. Int, 4 );
// Return Value
Command. Parameters. Add ("returnvalue ",
Sqldbtype. Int,
4, // size
Parameterdirection. returnvalue,
False, // is nullable
0, // byte precision
0, // byte Scale
String. empty,
Datarowversion. Default,
Null );

Command. Parameters ["@ categoryid"]. value = permission. categoryid;
Command. Parameters ["@ rolename"]. value = permission. permissionname;
Command. Parameters ["@ description"]. value = permission. description;
// Returns a new ID value.
Command. Parameters ["@ roleid"]. Direction = parameterdirection. output;

Int rowsaffected = command. executenonquery ();
Int result = command. Parameters ["returnvalue"]. value;
Int newid = command. Parameters ["@ roleid"]. value;

The function is quite powerful. We can get three values: Row impact value, stored procedure return value, and new id value.

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.