ADO. NET-basic summary and instance Introduction

Source: Internet
Author: User
Tags dot net

In some small programs written in my spare time recently, there are many accesses to the database. The following describes ADO. NET knowledge, there are deficiencies, hope you don't hesitate to enlighten me: mention ADO. NET, often with ASP. NET obfuscation, the two are very different, there is no comparability, the following describes the difference between the two before: ASP. NET is.. net Framework provides a class library for developing web applications, which is encapsulated in System. web. in dll, the corresponding program is System. web namespace. ADO. NET provides consistent access to data sources such as Microsoft SQL Server and XML and data sources exposed through OLE DB and XML. The ADO. NET class is in System. Data. dll and is integrated with the Xml class in System. XML. dll. To put it simply, ADO. NET is an access method used to process databases. The following describes the Connection class of the five common objects of ADO. NET that must be connected to the database for interaction. Connection help specifies the database server, database name, user name, password, and other parameters required to connect to the database. The Connection object will be used by the Command object to know which data source to execute the Command on. The process of interacting with the database means that the operation to be performed must be specified. This depends on the Command object. Developers use Command objects to send SQL statements to databases. The Command object uses the Connection object to indicate which data source to connect. A developer can use a Command object to directly execute commands, or pass a reference to a Command object to DataAdapter. It saves a group of commands that can operate the data described below. The Code is as follows: using (SqlConnection cnn = new SqlConnection ("data source = 10.10.198.111; Database = systemconfig; uid = sa; password = sa") {cnn. the string in the Open ();} SqlConnection parameter is the connection database address, database name, user name, password, and other information. It is usually stored on the web. in the config configuration file, you can directly reference the configuration information when calling it. The Code is as follows: <! -- Database connection address --> <connectionStrings> <add name = "connstr" connectionString = "data source = 10.10.198.111; Database = systemconfig; uid = sa; password = sa "/> </connectionStrings> private static string connstr = ConfigurationManager. connectionStrings ["connstr"]. connectionString; the Connection string used to access the database. The format is as follows: -- access Connection format Provider = Microsoft. ACE. OLEDB.12.0; Data Source = "+ Directory. getCurrentDirectory () + (@ "\ Areafull. accdb After the Command object is successfully connected to the data, you can use the Command object to execute commands such as query, modification, insertion, and deletion. commonly used methods of the Command object include the ExecuteReader () method, ExecuteScalar () method and ExecuteNonQuery () method. You can use the ExecuteNonQuery () method to insert data to execute the INSERT command. The Code is as follows: copy the code using (SqlConnection cnn = new SqlConnection (connstr) {cnn. open (); using (SqlCommand cmd = cnn. createCommand () {cmd. commandText = SQL; // execute the SQL statement cmd. parameters. addRange (parameters); // SQL parameter} copy the code DataAdapter class. In some cases, the data used by developers is mainly read-only and Developers rarely need to change it to the underlying data source. In the same situations, you need to cache data in the memory to reduce the number of database calls that do not change. DataAdapter disconnects the model to help developers easily process the above situations. When a single batch of read/write operations on the database are continuously changed and returned to the database, the DataAdapter fills in the (fill) DataSet object. DataAadapter includes a reference to the connection object and automatically enable or disable the connection when reading or writing data to the database. In addition, DataAdapter contains Command object references for SELECT, INSERT, UPDATE, and DELETE operations on data. The developer will define a DataAadapter for each Table in the DataSet. It will take care of all connections to the database for the developer. Therefore, developers tell DataAdapter when to load or write data to the database. The Code is as follows: copy the code using (SqlConnection cnn = new SqlConnection (connstr) {cnn. open (); using (SqlCommand cmd = cnn. createCommand () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); SqlDataAdapter apter = new SqlDataAdapter (cmd); DataSet ds = new DataSet (); apter. fill (ds); return ds. tables [0] ;}} copy the code DataSet object is the representation of data in the memory. It includes multiple able objects, and DataTable contains columns and rows, just like a table in a common database. Developers can even define the relationship between tables to create a master-slave relationship (parent-child relationships ). DataSet is used in specific scenarios-it helps to manage data in memory and supports data disconnection. DataSet is the object used by all Data Providers, so it does not need a special prefix like Data Provider. DataTable is a virtual grid table that temporarily saves data (a table that represents data in memory .). DataTable is the core object in the ADO dot net Library.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.