Ado. NET Foundation consolidates-----Connection classes and non-connected classes

Source: Internet
Author: User

The recent period of time their own state is still good, morning, running, on the self-study reading, the afternoon dormitory programming in real practice, the evening either exercise code, or go to play (in the not hit on the opportunity), life is quite rich.

On the basis of the review of C # first go to the front where, these to their own work slowly to experience, not that reading can be mastered. We all come from the school age to know that everyone's learning situation is not the same, so find their own learning rhythm is the best.

The following is about access to the database [ADO] learning, before the beginning of the study of these basic all over again, but for a long time not to use, some basic usage will still be forgotten.

One: The explanation of the basic name in ADO

Now I know that there are two types of connection classes and disconnection classes in ADO, which are distinguished from some basic keywords (called first).

Connection class:

Connection, Command, DataReader and DataAdapter.

The connection class can extract and update data from the underlying data source. Each data provider is responsible for implementing the connection class.

    • Connection

The Connection object represents a separate session with the data source, and the connection class specifies the authentication information that is required to connect to the data source.

    • Command

The Command object uses an established connection object to execute SQL statements and stored procedures against the data source. The CommandText property contains the SQL statements that are executed against the data source.

    • DataReader

DataReader provides only pre-and read-only access to the result set. The DataReader object provides the best performance for accessing data, avoiding the overhead associated with the dataset. You can create a DataReader object by executing the ExecuteReader () method of the command class. We can read the specific values in this object. There's A. Reader () method that we can get through this.

    • DataAdapter

DataAdapter by extracting data from the data source and populating a (disconnected) dataset/datatable, this can also be updated and reversed.

To disconnect a connection class:

Datasets, DataTable, Datacolumn,datarow, DataView, DataRelation and constraint.

The Disconnect class can access and manipulate the data provided by the connection class offline, and later synchronize with the underlying data source.

    • DataSet

A dataset is a disconnected relational database stored in memory (not related to the local), providing advanced browsing capabilities as a container for other objects, including Datatable,datarows,datacolumn, which we can use in our code.

    • DataTable

A DataTable represents a single table of data that is loaded into memory and can exist independently or as part of a dataset.

    • DataRow

DataRow represents a row of data in a DataTable

    • DataColumn

DataRow represents a column of data in a DataTable

For the above method, the basic operation of the property

    <summary>//Learn about connection classes and disconnected classes in ADO. </summary> class Program {static void Main (string[] args) {string connectionstr            ing = "";            String sql = "";            SqlConnection conn=new SqlConnection (connectionString);            SqlCommand cmd=new SqlCommand (sql,conn);   Cmd.commandtype=commandtype.text;                       This can be modified, if the stored procedure is directly modified, this is the enumeration type. 1: Use SqlDataReader to operate the Conn.            Open (); SqlDataReader dr = cmd.      ExecuteReader ();            Return Result: System.Data.SqlClient.SqlDataReader object. Dr.            Read ();            The data we need can be taken out below, if we need multiple while () to loop. String Data1=dr. GetString (0);            One row of data appears at a time. Conn.            Close (); Dr.                      Close (); 2: Use Sqldataapadter to operate the Conn.            Open ();            Declaring a DataTable and dataset they are disconnected classes, equivalent to a local database.            DataTable table = new DataTable ();            DataSet ds=new DataSet (); SqlDataAdapterDa=new SqlDataAdapter (CMD); Da.       Fill (table); Load the data into the DataTable, Da.          Fill (DS); Load the data into the dataset, Conn.            Close ();            We can find the data to use in the DataTable and dataset in the case where it is closed.            3: Some common methods of CMD. ExecuteScalar () + executes the query and returns the first column of the first row in the result set returned by the query.            Other columns or rows are ignored. Object rowcolumn = cmd.            ExecuteScalar ();            ExecuteNonQuery () + executes Transact-SQL statements on the connection and returns the number of rows affected. int Row=cmd.            ExecuteNonQuery ();            ExecuteReader () + sends CommandText to connection and generates a SqlDataReader and returns. SqlDataReader SQA = cmd.            ExecuteReader (); Conn.        Close (); }    }

ADO. NET Foundation consolidates-----Connection classes and non-connected classes

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.