Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use connection mode to access data in database in ADO.NET

Shulou Source: shulou.com Published: 2022-06-02 08:20:57 10月03日 Update

This article introduces the relevant knowledge of "how to use connection mode to access data in the database in ADO.NET". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The ADO.NET framework supports two modes of data access: connection mode (Connected) and non-connection mode (disconnected).

Data access of ADO.NET connection mode

Three core classes to be used for data access in the ADO.NET connection mode:

◆ IDBConnection: represents the connection to the data source, the base class of all Connection classes. SqlConnection implements the IDBConnection interface, which is used to connect with SQLServer data sources.

◆ DBCommand: represents the base class of all Command classes. SqlCommand implements the IDBCommand interface with a Transact-SQL statement or stored procedure to execute against the SQLServer database.

◆ DataReader: the base class for all DataReader classes, SqlDataReader implements the IDataReader interface, providing a stream-only way to read rows from an SQLServer database.

If you want to connect to a Microsoft SQLServer database, try to use the SqlConnection,SqlCommand,SqlDataReader class in the SqlClient namespace; if you communicate with the Oracle database, you should use the class of the OracleClient namespace; if you communicate with other databases, you should use the class of the OleDB or ODBC namespace.

An example of data access for a simple ADO.NET connection mode:

Code namespaceDawnDataObject {publicclassMovies// data entity object {publicstaticreadonlystring_connectionString;// connection database string is a static member and is shared by each instance. StaticMovies () {_ connectionString=WebConfigurationManager.ConnectionStrings ["DawnEnterpriseDBConnectionString"]. The properties included in the ConnectionString;} privatestring_title; privatestring_director; / / Movies class are Title, Director publicstringTitle {get {return_title;} set {_ title=value;}} publicstringDirector {get {return_director;} set {_ director=value;}} / / the GetAll method in the class returns a List object, which can be bound publicListGetAll () {Listresult=newList (); SqlConnectionconn=newSqlConnection (_ connectionString) as a data source by controls such as GridView. The SqlCommandcomm=newSqlCommand ("selectTitle,DirectorfromMovies", conn); using (conn) {/ / using keyword specifies that once conn leaves this code segment, its Dispose function conn.Open (); SqlDataReaderreader=comm.ExecuteReader (); while (reader.Read ()) {Moviesnewmovie=newMovies (); newmovie._title= (string) reader ["Title"]; newmovie._director= (string) reader ["Director"]; result.Add (newmovie);} returnresult } "how to use connection mode in ADO.NET to access data in the database" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Data database schema object interface data source space content more knowledge process communication utility learning and then three code examples keywords. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Shulou Technology Xiaomi Docker