Article 1: Concepts 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 contains the oledbconnection class and sqlconnection class, respectively for different databases. Sqlconnection is applicable to SQL Server 7.0 or later.
2 command class
The database command class encapsulates database commands. This command can be an SQL statement or a stored procedure. This class also has a specific provider-related prefix, such as oledb or SQL. All command classes must implement some attributes to change the command text and type, parameters, timeouts, and transactions. In addition, Comand must implement some methods to execute command and return information.
3 dataadapter class
Dataadapter is generally used in combination with dataset. The dataadapter object is used to retrieve data from the data source and fill the tables in the dataset.
Essentially, dataadapter is a container that contains four pre-configured command instances: selectcommand, insertcommand, deletecommand, and updatecommand. The four command instances provide operations between dataset and databases.
4 datareader class
Datareader can be used to achieve high-speed and forward-only access to data in the data source. Datareader is a dependency connection object, which means that you must keep the database connection open when using it.
5 dataset class
Relatively complex but powerful class. The following is a detailed description.
The following example demonstrates basic operations (we use the SQL Server database ).
Using system;
Using system. Data;
Using system. Data. sqlclient;
Namespace test101
{
///
/// Summary of class1.
///
Class class1
{
///
/// Main entry point of the application.
///
[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 sqlconnection object Conn, assign the database connection string to its constructor, and open the database connection through the open method.
Sqlcommand cmd = conn. createcommand (); // create a sqlcommand using the createcommand method of Conn
Cmd. commandtext = "select top 5 * from MERs"; // the command for setting the CMD object is to read the first five pieces of information in the database summary.
Cmd. commandtype = commandtype. Text; // set the type of CMD to SQL statement, that is, the default type.
// Of course, we can use cmd. commandtype = commandtype. storedprocedure to specify the command type as the stored procedure.
// The following code uses the executereader method of CMD to create a sqldatareader object.
// Note: datareader does not have its own construction function and can only be created through the executereader of CMD.
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 and display the information. Next we will introduce the datareader class
Console. writeline (output );
}
}
}
}
The interface is as follows:
In the next article, we will study the datareader class in detail.