In the process of using ASP. NET, database access is a very important part. Especially during the construction of the B/S system, database operations have almost become an essential operation. Calling stored procedures to perform database operations is a method that many programmers use. This article will work with you to learn how to use stored procedures in ASP. NET database applications to improve the overall performance of ASP. NET database applications.
1. About stored procedures
Stored Procedure is a set of SQL statements for specific functions. It is compiled and stored in the database. You can specify a stored procedure name and provide parameters (if the stored procedure has parameters)
To execute it. Stored procedures are an important object in databases. Any well-designed database application should use stored procedures.
1. performance:
● Stored procedures provide advanced features not available in many standard SQL languages. The function of passing parameters and executing logical expressions helps Application designers process complex tasks.
● A stored procedure provides a security mechanism to grant users the permission to access the stored procedure, rather than the permission to access the tables involved in the stored procedure, this ensures data integrity in the database.
● After the first execution of the stored procedure, a query tree will be created in the SQL Server Buffer, so that no compilation is required during the second execution of the stored procedure, therefore, the execution speed is much faster than that of SQL statements in a program.
● The stored procedure is stored on the local server, reducing the network load and the execution time required to execute the process.
2. program structure:
● In terms of program scalability, the use of stored procedures will facilitate subsequent modifications to the program. For example, if the database structure changes, you only need to modify the corresponding storage structure and the calling part of the program.
● A stored procedure can be regarded as a function module written by the user to complete a specific function. users or other users can reference the stored procedure in their other stored procedures, so as to realize code reusability and improve the development quality and efficiency.
From the analysis above, we can see that it is necessary to use stored procedures in applications.
3. Stored Procedure type
Depending on the type of the returned value, we can divide the stored procedure into three types: the stored procedure of the returned record set, the stored procedure of the returned value (also known as the scalar stored procedure), and the behavior stored procedure. Returns a record, as its name implies.
The execution result of the stored procedure of the set is a record set. A typical example is to retrieve records that meet one or more conditions from the database; returns a value after the stored procedure of a value is executed.
Return Value Function or command. Finally, the behavior stored procedure is only used to implement a function of the database without returning values, such as updating and deleting operations in the database.
2. Use stored procedures in ASP. NET
In ASP. NET, the application stored procedure needs to access the database in the application. The general step is: first declare a database to connect to sqlconnection, and then declare a database command sqlcommand to execute
SQL statements and stored procedures. With these two objects, you can use different execution methods as needed. For the execution of a stored procedure, if the first type of stored procedure is executed,
Dataadapter fills in the result to a dataset, and then you can use the data grid control to display the result on the page. If the second and third stored procedures are executed, this process is not required. You only need to determine whether the operation is successful based on the specific response.
1. Simple Stored Procedure Application
(1) create a simple stored procedure and create a new stored procedure named my_proc in the SQL server's built-in northwind database. The stored procedure is returned to an order destined for the United States.
The command is as follows:
USE NorthwindGoCreate PROC my_procAsSELECT * FROM order WHERE ShipCountry='USA'Go
(2) create an application that calls a simple stored procedure:
The following describes how to call this stored procedure through a program. Create a file named storepro. aspx, which uses the sqldataadapter object and DataSet object in ADO. net. Sqldataadapter object
As a bridge between the SQL Server database and the DataSet object, the two are linked together. The sqldataadapter object contains two common methods: the fill () method and the update () method. The fill () method can
The database obtains the corresponding data and fills it in the DataSet object. Before calling the fill () method, we must set the selectcommand attribute of the sqldataadapter object. This attribute is actually a sqlcommand
Object. The selectcommand attribute contains valid SQL statements, which can be obtained from the database and filled into the DataSet object.
The program code is as follows:
Default. aspx page:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
Default. aspx. CS:
Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. data. sqlclient; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {sqlconnection sqlcon = new sqlconnection (); sqlcon. connectionstring = "Data Source = localhost; initial catalog = northwind; user id = sa; Password = 123"; sqldataadapter SDA = new sqldataadapter ("my_proc", sqlcon ); // call the stored procedure "my_proc" SDA. selectcommand. commandtype = commandtype. storedprocedure; // set the command type to dataset DS = new dataset (); SDA. fill (DS, "orders"); gridview1.datasource = Ds. tables ["orders"]; gridview1.databind ();}}
3. Summary:
This article discusses the basic knowledge of stored procedures and illustrates through examples how to combine sqldataadapter objects, dataset objects, sqlparameter objects, and other data-driven applications in ASP. NET database applications. It is not difficult to find that encapsulating data update business logic in the stored procedure is a good design method, which can improve the manageability, scalability and database security of applications.
Link: http://www.cnblogs.com/gisera/articles/1266697.html