[Repost] ASP.NET Lecture (5) - Database ADO.NET

xiaoxiao2021-03-06  19

Since a few years ago, a variety of database access technologies have occurred since a number of years ago, and ADO.NET has the latest one. In this process, many interesting things have occurred. For example, COM breaks into the database field and starting to cultivate the colonial process of OLE DB. Then, approximately equivalent to the OLE DB automation version of ActiveX? Data Objects (ADO) is selected to rule the Visual Basic® and ASP community of the Windows database developer.

Through the .NET, Microsoft is providing a general-purpose frame (ie, Framework Class Library), which will include all existing Windows APIs or even more. It is particularly worth mentioning that it includes a large number of commonly used libraries, and these libraries need to be obtained separately by each COM object. In these libraries, you will find XML and ADO object models, which are integrated into classes called ADO.NET.

ADO.NET is in fact becoming the basis for building data perception .NET applications. Unlike ADO, ADO.NET follows the more general principles, not so special for the database. ADO.NET gathers all classes that allow data processing. These classes represent data container objects with typical database functions such as index, sorting, and views. Although ADO.NET is an authoritative solution for .Net database application, it is not like the ADO model as a database center, which is a major feature of ADO.NET.

ADO.NET has great differences from ADO. ADO.NET is a new data access programming model that requires a comprehensive understanding, investment and new thinking of developers. However, once you start to master ADO.NET, you will realize that the original ADO skills are very helpful to create effective applications and resolve all kinds of old problems in different, more clever and reliable ways. (Speech translated from an official of Microsoft .NET department)

5.1 Current ADO.NET

At present, ADO.NET provides two hosting providers: a use of SQL Server 7.0 or higher, another for all OLE DB providers that you may have installed. In both cases you use different classes, but follow similar naming rules. In addition to the prefix, the name is the same. The preceding case is SQL, and the latter case is ADO.

<% @ Import namespace = "system.data.ado"%> <% @ import namespace = "system.data.sql"%>

You should use the SQL class to access the SQL Server table because they directly enter the internal API of the database server, skip the intermediate layer represented by the OLE DB provider. The ADO class is the .NET interface on the OLE DB provider, which works using the COM InteroP bridge.

5.2 Connecting a database

DIM MyConnection As New SqlConnection ("Server = localhost; uid = sa; pwd =; database = pubs") Dim MyCommand as new SqlDataSetCommand ("Select * from authors", MyConnection)

Or SQLConnection myConnection = new SQLConnection (); myConnection.DataSource = "localhost"; myConnection.UserID = "sa"; myConnection.Password = ""; myConnection.ConnectionTimeout = 30; myConnection.Open (); myConnection.Database = "pub "; MyConnection.IsolationLevel = isolationledLevel.Readcommitted Here we need to talk about the method and attribute of the connection.

ConnectionTIMEOUT timeout

Database default database

DataSource DNS

Userid is originally called UID

Password

State gets the state of current connection

Open () open

Close () Close 5.3 Operation Database

Through a Command object, we can operate the database.

Dim myConnection As SQLConnection = New SQLConnection ( "server = localhost; uid = sa; pwd =; database = pubs") Dim myCommand As SQLCommand = New SQLCommand ( "select * from Authors", myConnection) myConnection.Open () Dim dr As New SqlDataReadermyCommand.execute (DR)

...

MyConnection.Close ()

Or do this DIM MyConnection As New SqlConnection ("Server = localhost; uid = sa; pwd =; database = pubs") Dim MyCommand as new sqlcommand (_ "Update Authors Set Phone = '(800) 555-5555' WHERE AU_ID = '123-45-6789' "", _myconnection) myCommand.activeConnection.Open () Mycommand.executenonQuery () MyCommand.activeConnection.Close ()

These are the standard usage of SQLCommand, and all of the properties and related methods of Command are listed below.

ActiveConnection acquisition or set up links Connections

CommandText execution SQL statement or storedProcedure name

CommandTimeout timeout

CommandType Command's Type (StoredProcedure, TEXT, TABLEDIRECT), default Text

PARAMETERS is used when the storage process is stored

Execute () executes SQL statements or storage procedures

ExecutenonQuery () is above, but there is no return, or said only the number of records

Note: Like ASP, be sure to close the Connection after running, otherwise the server resources will be consumed.

5.4 Display of data

Before the explanation of this section, we build a database first, named the ASPNet and there is a table User structure in the following:

UIDUSERNAMEMAIL1 USER1MAIL12 USER2MAIL23 USER3 MAIL3SQL Statement Select * from User

Database statement server = localhost; uid = sa; pwd =; database = aspnet

5.4.1 Display data with DataReader method

There are two ways to display data DataReader methods, and DataSet methods, and DataReader can only store query data, let us first use the DataReader method to display