I have recently read the efficiency of Wrox, ADO.NET, I feel, I hope to share with you. For the first time, write an article, not good, please understand.
First: ADO.NET concept
ADO.NET includes the following common classes:
· Connection
· Command
DataAdapter
DataReader
· DataSet
1 Connection class Database The Connection class provides connections to the database. The .NET has an OLEDBConnection class and a SqlConnection class, which are directed to different databases. SqlConnection is for SQL Server 7.0 or above.
2 Command Class Database Command class is a package of database commands. This command can be a SQL statement or a stored procedure. This class also has a prefix associated with the specific provider, such as OLEDB or SQL. All Command classes must implement some properties to change the text and types, parameters, timeouts, and transactions of Command. In addition, Comand must implement some methods to perform Command and return information.
3 DataAPter class DataAdarpter is generally used in conjunction with DataSet, the DataSet "Connect" to the data source. Essentially DataAdapter is a container, contains 4 pre-configured Command instances, which is SelectCommand, INSERTCOMMAND, DELETECMMAND, UpdateCommand. These four Command instances will provide operations between DataSet and databases.
4 DataReader class Use DataReader to implement high speeds of data from the data source, only forward access. At the same time, DataReader is an object that relies on the connection, which means that you must keep the database connection when you use it.
5 DataSet classes are relatively complex but powerful classes. The introduction will be described later.
Below will now demonstrate basic operations (we use SQL Server Database).
Using system;
Using system.data;
Using system.data.sqlclient;
Namespace Test101
{
///
/// Class1 summary description.
///
Class class1
{
///
/// The main entry point for 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, connect the database connection The character is serially assigned to its constructor and open the database connection via the OPEN method.
Sqlcommand cmd = conn.createCommand (); // creates a SQLCommand cmd.commandtext = "Select Top 5 * from customers"; // Setting the CMD object is the acquisition of 5 information cmd before reading the database. CommandType = commandType.text; // Set the type of CMD is a SQL statement, that is, the default type / / Of course we can use cmd.commandType = commandType.StoredProcedure Specify the command type as a stored procedure. // The following code uses the CMD's EXECUTEREADER method to create a SQLDataReader object. // Note: DataReader does not have its own constructor, can only create new CMD's ExecuteReader. 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), read.getstring (2)); // read the information and display. Later, we will specifically introduce the DataReader CONSOLE.WRITELINE (OUTPUT);}}}} The interface is as follows:
Next article we will study DataReader class in detail