A detailed study of ado.net (i)

Source: Internet
Author: User
Tags implement
ADO recently read the Wrox efficient Master ado.net, have feelings, hope to share with you. The first time to write an article, not please understand.

Chapter One: the concept of ado.net

Ado. NET contains the following common classes:

· Connection

· Command

· DataAdapter

· DataReader

· DataSet

1 Connection Class
The database connection class provides a connection to the database.. NET has OleDbConnection classes and SqlConnection classes, respectively, for different databases. SqlConnection is for SQL Server 7.0 or above.

2 Command class
The database command class is the encapsulation of database commands. This command can be either an SQL statement or a stored procedure. This class also has a prefix associated with a specific provider, such as OLE DB or SQL. All command classes must implement properties that change the command's text and type, parameters, timeouts, and transactions. In addition, comand must implement some methods to execute command and return information.

3 DataAdapter Class
Dataadarpter is generally used in conjunction with a dataset, which is "connected" to the data source.
Essentially DataAdapter is a container that contains 4 pre-configured command instances, that is, Selectcommand,insertcommand,deletecommand,updatecommand. These 4 command instances provide an operation between the dataset and the database.

4 DataReader Class
You can use DataReader to achieve high-speed, forward-only access to data in a data source. At the same time DataReader is a dependent object, which means that you must keep the database connection open when you use it.

5 DataSet class
A relatively complex but powerful class. The details are given later.

The following examples demonstrate basic operations (we use SQL Server databases).

Using System;

Using System.Data;

Using System.Data.SqlClient;



Namespace test101

{

<summary>

Summary description of the CLASS1.

</summary>

Class Class1

{

<summary>

The main entry point for the application.

</summary>

[STAThread]

static void Main (string[] args)

{

//

TODO: Add code here to start the application

//



SqlConnection conn = new SqlConnection ("server = joycode;initial Catalog = Northwind; User Id = sa; Password = 87345587; ");
Conn. Open ();
The above two lines of code create a new SqlConnection object conn and assign the database connection string to its constructor and open the database connection through the Open method.

SqlCommand cmd = conn. CreateCommand ()//through the Conn CreateCommand method to establish a SqlCommand
Cmd.commandtext = "SELECT Top 5 * Customers";/the command to set the CMD object is to read the first 5 information of the database rollup
Cmd.commandtype = commandtype.text;//The type of CMD is the SQL statement and is the default type
Of course we can use Cmd.commandtype = CommandType.StoredProcedure to specify that the command type is a stored procedure.
The following code creates a new SqlDataReader object using the ExecuteReader method of CMD.
Note: DataReader does not have its own structure function, only through the ExecuteReader of cmd new.
SqlDataReader reader = cmd. ExecuteReader (commandbehavior.closeconnection);
string output;
while (reader. Read ())
{
Output = string. Format ("Customer {0}: {1} works for {2}",
Reader. GetString (0), reader. GetString (1), reader. GetString (2));//Read the information and display it. We will introduce the DataReader class in the following
Console.WriteLine (output);
}
}
}
}


The interface is as follows:



In the next article we will study the DataReader class in detail


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.