Application of ADO. NET --- learning notes

Source: Internet
Author: User

ADO. NET application ---- here, I will share my experiences with you. If you do not understand or have better insights, you can contact me to leave a message, we believe that our knowledge can be improved.
First, let's take a simple look at ADO. NET is an object-oriented class library used to interact with data sources. It has several important objects, such as Connection, Command, DataReader, DataAdapter, and DataSet. (Declare that since I use the SQL database, I will show you the operations of ADO. NET and SQL)
Connection: Mainly used to connect to the database.
For example: SqlConnection conn = new SqlConnection (@ "Data Source = SMALLZ-PC \ SQLEXPRESS; database = ado. bet; uid = sa;
Pwd = zz326155 "); // Data Source represents the database name uid of the server, and pwd represents the username and password respectively.
Conn. Open ();

 

Command: mainly refers to the object that sends SQL statement commands to the database for addition, deletion, modification, and query.
For example, SqlCommand cnm = new SqlCommand (); // instantiate a SqlCommand object
Cnm. Connection = conn; // create a Connection to the database


Database <-> Command <-> application at this time, Command is equivalent to a person who sends commands in the middle.

There are three main methods in Command:
1. ExecuteNonQuery: Execute an SQL statement and return the number of affected rows.
2. ExecuteReader: Execute an SQL statement and generate an example of a SqlDataReader object containing data.
3. ExecutScalar: Execute the SQL statement and return the first column () in the first row of the result set ().


DataReader: A simple dataset used to retrieve read-only datasets from data sources. It is often used to retrieve a large amount of data. DataReader objects can only be read-only and forward
You can view the data stored in it in a very efficient data view mode. Meanwhile, the DataReader object is a resource-saving data object.
DataReader is often used to retrieve data from the data source through the ExecuteReader method of the Command object.
For example, SqlConnection conn = new SqlConnection ();
Conn. ConnectionString = @ "Data Source = SMALLZ-PC \ SQLEXPRESS; database = ado. bet; uid = sa; pwd = zz326155 ";
// Open the connection www.2cto.com
Conn. Open ();
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = conn;
Cmd. CommandText = "SELECT Name FROM Table_1 ";
SqlDataReader drNew = cmd. ExecuteReader ();
If (drNew. HasRows)
{
While (drNew. Read ())
{
Console. WriteLine (drNew [0]. ToString ());
}
}


DataAdapter: a set of SQL commands connected to a database. They are used to fill in DataSet and update data sources. Namespace: System. Data. Common


DataSet: it is equivalent to a small database in memory.
In practical applications, DataSet is generally used in three ways:
1. Fill the data in the database with DataSet through the DataAdapter object.
2. update the database through DataSet operations on the DataAdapter object.
3. load XML data streams or text to DataSet.

 

From the dark @ fascinating

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.