Simple ADO. NET knowledge sorting

Source: Internet
Author: User

ADO. NET
Consumer objects

Dataset: data is stored in memory. DataSet is a Dataset, including
DataTable and DataRelation, and DataTable contains DataRow and DataColumn. After the data is read to DataSet, you can perform CRUD operations on the DataTable as an index or table name.

. NET
Data Provider objects

Connection: database Connection object.

Purpose: 1. Connect to the database; 2. Create a Command object.

For example:

SqlConnection thisConnection = new SqlConnection (@ "server =. \ sqlexpress; Integrated Security =
True; Database = C: \ SQL SERVER 2000 SAMPLE DATABASES \ NORTHWND. MDF ");

ThisConnection. Open ();

SqlCommand thisCommand =
ThisConnection. CreateCommand ();

Command: database Command object. The Command type can be an SQL statement or a stored procedure.

For example:

ThisCommand. CommandType = CommandType. StoredProcedure;

ThisCommand. CommandText = "Ten
Most Expensive Products ";

CommandBuilder: used to build SQL commands. It is often used in combination with DataAdapter objects to automatically generate SQL statements.

For example:

SqlDataAdapter thisAdapter = new
SqlDataAdapter ("select
CustomerID from Customers ", thisConnection );

SqlCommandBuilder thisBuilder = new SqlCommandBuilder (thisAdapter );

You can obtain the CRUD method automatically generated in the CommandBuilder object.

DataReader: used to read only forward and read-only data. This object has the best performance for simple data reading.

For example:

SqlCommand thisCommand =
ThisConnection. CreateCommand ();

ThisCommand. CommandText = "select
CustomerID, CompanyName from Customers ";

SqlDataReader thisReader =
ThisCommand. ExecuteReader ();

DataAdapter: a bridge between the database and memory. It reads data from the database to Fill the DataSet object. The Fill () method is his.

DataSet thisDataSet = new
DataSet ();

SqlDataAdapter custAdapter = new
SqlDataAdapter ("select
* From MERs ", thisConnection );

CustAdapter. Fill (thisDataSet, "MERs ");

After the data is filled into the DataSet object, you can operate on one of the tables and perform CRUD operations on each row and column of a table.

 

ADO. NET and XML

You can write data from a DataSet object to an XML file:

For example:

DataSet thisDataSet = new
DataSet ();

OrderAdapter. Fill (thisDataSet, "Orders ");

ThisDataSet. WriteXml (@ "d: \ tmp \ nwinddata. xml ");

Of course, you can also read data from an XML file to a DataSet object:

For example:

DataSet thisDataSet = new
DataSet ();

ThisDataSet. ReadXml (@ "d: \ tmp \ nwinddata. xml ");

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.