ADO. NET -- Connection, Command ., Ado. netconnection

Source: Internet
Author: User

ADO. NET -- Connection, Command ., Ado. netconnection

The. NET Framework Data Provider includes four core objects: Connection, Command, DataReader, and DataAdapter. Here, I will just talk about the objects that are frequently used in SQL Server. The connection object SqlConnection, which is used in the namespace of System. Data. SqlClient.

1. Connection object

Description: As the first core object of the Data Provider, the Connection object is responsible for connecting to the Data source.

Attribute:

1. DataBase: the name of the current DataBase can be obtained after the connection is opened, or the DataBase name specified by the connection string can be obtained before the connection is opened.

2. DataSource: get the name of the database server to be connected.

3. ConnectionString: gets or sets the string used to open the connection.

4. ConnectionTimeOut: gets the waiting time before the connection is established to terminate the attempt and generate an error.

5. State: gets the description string.

Method:

1. open: Use the settings specified by ConnectionString to open the database connection.

2. Dispose: release all resources specified by Component.

3. Close: Close the connection to the database.

If the data source is compared to the door, the connection string is the key, and the connection object is the person who opens the door with the key.

Ii. Command object

Description: although the Connection object has been connected to an external data source, it is loyal to its duties and does not provide any operations on the external data source. At the very moment of tangle, the Command object was born.It encapsulates all operations on external data sources (including addition, deletion, query, modification, and other SQL statements and stored procedures) and returns appropriate results after execution.

Attribute

1. CommandText: Get or set the text Command executed on the data source. The default value is an empty string.

2. CommandType: indicates the command type or specifies how to interpret the CommandText attribute. The value of the CommandType attribute is of the enumeration type. The definition process is as follows:

<Span style = "font-size: 18px;"> Public enum CommandType {Text = 1; // SQL Text command StoreDProcedure = 4; // name of the stored procedure TableDirect; // table name} </span>


Note that,When you set CommandType to StoredProcedure, set the CommandText attribute to the name of the stored procedure.

3. Paramenters: bind SQL statements or stored procedure parameters. Objects that are indispensable in parameterized queries.

4. Tranction: Get or set the Command object in which the. NET Framework data provider is executed.

5. Connection: set or obtain the Connection to the data source.

Method:

1. ExecuteNonQuery: execute the operation that does not return data rows and return an int type data. (For Update, Insert, and Delete statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is-1)

2. ExecuteReader: executes the query and returns a DataReader object.

3. ExecuteScalar: executes the query and returns the first column (object type) in the first row of the query result set ). If no result set is found, null reference is returned.

These two objects are used for Connection. The Connection object serves to connect to the data source. After the Connection is successful, the Command object is used to operate the data. The next blog will focus on DataAdapter objects.

Related Article

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.