Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report