Use of stored procedures in VB.net

Source: Internet
Author: User

In the IDC charging system, I tried to use the stored procedure, and it was a few days before the database was learned. I have heard of the stored procedure before, but I am not very clear about it. Therefore, write this blog!

Professional concept: stored procedures are a collection of SQL statements and control structures created in databases. Simply put, it is a set of SQL statements with fixed operations. Similar to functions or procedures in programming languages, they can be called directly by name. Can be created through T-SQL statements and Enterprise Manager. The T-SQL syntax is as follows:

Create procedure (create Stored Procedure) Create procedure stored procedure name (parameter list) Begin SQL statement code block end

The combination of VB.net and stored procedures has two obvious advantages. First, the execution speed of the stored procedure is fast. Because the stored procedure is pre-compiled. When a stored procedure is run for the first time, the optimizer analyzes and optimizes it and provides an execution plan that is finally stored in the system table. Second, stored procedures can reduce network traffic because calling stored procedures only requires a set of process names and parameters, while directly using stored procedures requires passing a large number of SQL statements to the database. In addition, security is also one of its advantages. By Assigning permissions for executing a stored procedure, you can restrict the access permissions to the corresponding data, avoiding unauthorized user access to the data, this ensures data security. At the same time, you can avoid database operation errors because it is pre-compiled.

In VB.net, The sqlclient. sqlcommand class is used to call the stored procedure:

Command = new sqlclient. sqlcommand ("Stored Procedure name", new sqlclient. sqlconnection ("connection string") command. connection. open () command. commandtype = commandtype. storedprocedure command. parameters. addrange (parameter set) command. executenonquery ()

Note the returned values of the command.exe cutenonequery method. For update, insert, and delete statements, the returned values are the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the returned value is-1.

The use of stored procedures can optimize the code structure and improve programming flexibility. Because we use stored procedures to separate database operations. This conforms to the concept of decoupling and thread. Dividing tasks into single-function, clear parts. This avoids the impact of database operation errors on the software.

 

 

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.