Ado. NET tutorial (1) Initial knowledge of ADO

Source: Internet
Author: User

ADO. NET IntroductionADO. NET use of the classmethods and properties to be masteredADO Connection Instance

Ado. NET's name originates from ADO (ActiveX data Objects) and is a library of COM components used to access data in previous Microsoft technologies. The ADO name is used because Microsoft wants to show that this is the preferred data provider in the net programming environment.

ADO allows developers to access data sources in a consistent way, such as SQL Server and XML, as well as data sources exposed through OLE DB and ODBC. The data sharing consumer application may use ADO to connect to these sources and to retrieve, process and update the information contained therein. The data management data access can be separated into discrete components, which can be used separately or in series with the. NET Framework Data Provider for connection to the repository, execution of commands, and retrieval of results. These results are processed directly into the ADO DataSet object in order to make use of machine operation (Ad Hoc) to expose the user, to data from multiple sources, or to pass between tiers. DataSet objects can also be used separately from the. NET Framework data Provider to manage the application's native data or data from XML. The ADO (Class) is located in System.Data.dll and is integrated with the XML category in System.Xml.dll. ADO provides developers writing Managed code similar to the functionality that ActiveX Data Objects (ADO) provides to native component object models (Component object model,com) developers. It is recommended to use ADO instead of ADO to access data in. NET applications. ADO. NET provides the most straightforward way for developers to access data in the. NET Framework.

  1. Class Library:Connection classTo interact with the database, you must connect to it. The connection help indicates the database server, database name, user name, password, and other parameters required to connect to the database. The connection object is used by the command object to know which data source is executing the command. The process of interacting with a database means that you must indicate the action you want to perform. This is done by the Command object. The developer uses the command object to send the SQL statement to the database. The command object uses the connection object to indicate which data source to connect to. Developers can use the command object alone to execute commands directly, or to pass a reference to a Command object to DataAdapter, which holds a set of commands that manipulate the set of data described below. [3]Command ObjectAfter the successful connection with the data, you can use the command object to execute query, modify, insert, delete and other commands; The common methods for command objects are the ExecuteReader () method, ExecuteScalar () method and ExecuteNonQuery () method; Insert data The ExecuteNonQuery () method can be used to execute the insert command. [3]DataReader classMany data operations require developers to simply read a string of data. The DataReader object allows the developer to obtain the results from the Command object's SELECT statement. Considering the performance factor, the data returned from DataReader is fast and is just a "forward" stream of data. This means that developers can only fetch data from the data stream in a certain order. This is good for speed, but if the developer needs to manipulate the data, a better approach is to use a dataset. [3]DataSet ObjectA DataSet object is a representation of the data in memory. It includes multiple DataTable objects, while a DataTable contains columns and rows, just like a table in a normal database. Developers can even define relationships between tables to create a master-slave relationship (parent-child relationships). Datasets are used in specific scenarios-helping to manage the data in memory and supporting the disconnection of data. The dataset is an object used by all data providers, so it does not require a special prefix like data provider. [3]DataAdapter ClassAt some point the data used by developers is primarily read-only, and developers rarely need to change it to the underlying data source. The same situation requires that data be cached in memory to reduce the number of times that unchanged data is called by the database. DataAdapter helps developers to easily handle the above situations by disconnecting the model. The DataAdapter fills the DataSet object when a continuous change in the read and write operation of a database is returned to the database in a single batch. The dataaadapter contains a reference to the Connection object and automatically opens or closes the connection when the database is read or written. In addition, DataAdapter contains a command object reference to the Select, INSERT, update, and delete operations of the data. The developer will define dataaadapter for each table in the dataset, which will take care of all connections to the database for the developer. So the work the developer will do is tell DataAdapter when to load or write to the database. [3]DataTable classA DataTable is a data grid control. It can be applied to VB and ASP. It can simply bind the database without code. It has a Microsoft-style user interface. Instantiation of a DataTable and adding columns: DataTable dt = new DataTable ();d T. Columns.Add ("ID");d T. Columns.Add ("Name");D Atarow dr = dt. NewRow (); object[] Objs = {1, "Name"};d R. ItemArray = Objs;dt. Rows.Add (dr); this.dataGridView1.DataSource = DT; [3]

1: Several methods that must be mastered

Open (): Opens the database connection with the property settings specified by System.Data.SqlClient.SqlConnection.ConnectionString

Dispose (): Releases all resources used by System.ComponentModel.Component.

Close (): Closes the connection to the database. This method is the preferred method for closing any open connections

2: Properties that must be mastered

Database: The name of the current database or the name of the databases to use when the connection is opened

DataSource: Gets the name of the instance of SQL Server to connect to

ConnectionString: Gets the time to wait before terminating an attempt and generating an error when attempting to establish a connection.

ConnectionString: Gets or sets the string used to open the SQL Server database

ConnectionState describes the current state of the connection to the data source.

Properties of the ConnectionState

///Summary://The connection is in a closed state. Closed =0,////Summary://The connection is open. Open =1,////Summary://the Connection object is connecting to the data source. connecting =2,////Summary://The Connection object is executing the command. (This value is reserved for future versions of this product.) )executing =4,////Summary://The Connection object is retrieving data. (This value is reserved for future versions of this product.) )fetching =8,////Summary://the connection to the data source was interrupted. This behavior can occur only after the connection is open. You can turn off the connection in this state and then turn it back on. (This value is reserved for future versions of this product.) )Broken =16

ADO Connection Instance

  stringstrSQL ="Data source=.; Database=ttest; User id=sa;pwd=123456"; SqlConnection Connection=NewSqlConnection (strSQL); Connection.            Open (); //determine if the database is open            if(Connection. state = =ConnectionState.Open) {Console.WriteLine ("Data connection Information"); Console.WriteLine ("Database Source: {0}", connection.                DataSource); Console.WriteLine ("database name: {0}", connection.                Database); Console.WriteLine ("time spent connecting data: {0}", connection.            ConnectionTimeout); }            //Close Connectionconnection.            Close (); //Release All Resourcesconnection.            Dispose (); if(Connection. state = =connectionstate.closed) {Console.WriteLine ("database is down"); }

Ado. NET tutorial (1) Initial knowledge of ADO

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.