Use the stored procedure to perform database operations

xiaoxiao2021-03-06  68

The stored procedure is like a function and sub-process in its actual programming language, contains certain content, stored in a database in a database, convenient for users to call at any time.

There are many benefits to using stored procedures to perform database operations:

1. Save in the database in the form of compilation, so the speed of execution is fast

2. Allow modular programming to make it easy to modify

3. Save network bandwidth

4. Improve security

This is established a simplest stored procedure, and the database is Northwind

Create Procedure My.StoredProcedure AS

Select Firstname, Lastname

From

Employees

Return

After saving, you can find a memory process in the database in the database.

Create a new web form, the code is as follows

Private Void Page_Load (Object Sender, System.EventArgs E)

{

SqlConnection

Conn

= New SQLCONNECTION ("Server = .; data = northwind; uid = sa; pwd = chengbo;");

Sqlcommand comm = new sqlcommand ();

Comm.Connection =

Conn

;

Comm.commandtype = commandtype.storedProcedure;

Comm.comMandText = "sp_select_allemployees";

Cn.open ();

SqldataReader Reader = Comm.ExecuteReader ();

DataGrid1.datasource = reader;

DataGrid1.databind ();

CONN.CLOSE ();

}

Press F5 to run, everything is normal.

转载请注明原文地址:https://www.9cbs.com/read-86999.html

New Post(0)