Summary of four objects in ADO. Net

Source: Internet
Author: User

What is ADO. Net and what is it? ADO. Net: the interaction between a program and a database must be performed through ADO. Net, and the SQL statement can be executed in the program through ADO. Net. ADO. Net provides unified operation interfaces for different databases. The SQL Server database (service-based database) is directly embedded with mdf files in the project ). The mdf file is easy to use as the project goes. It is no different from creating a database on the database server. It is automatically added (Attack) during running ). Four objects of ADO. NET (1) SqlConnection object connection string: The program uses the connection string to specify the server on which the connection is established, the database of which instance, and the username and password used. Connection string in mdf file format embedded in the project: "DataSource =. \ SQLEXPRESS; AttachDBFilename = | DataDirectory | \ Database1.mdf; IntegratedSecurity = True; User Instance = True ". ". \ SQLEXPRESS" indicates "SQLEXPRESS instance on the local machine", which needs to be modified ." Database1.mdf "is the mdf file name. In ADO. Net, a connection is created to SQLServer through the SQLConnection class. SqlConnection represents a database connection, and connections and other resources in ADO. Net all implement the IDisPosable interface. You can use using for resource management. Example: code for connecting to SQlServer: [csharp] using (SqlConnection conn = new SqlConnection (@ "Data Source =. \ SQLEXPRESS; ttachDBFilename = | DataDirectory | \ Database1.mdf; Integrated Security = True; User Instance = True ") {conn. open () ;}( 2) SqlCommand object SqlCommand indicates that the CommandText attribute of a command (SQL statement, etc.) submitted to the server is the SQL statement to be executed. Main SqlCommand method: (a) ExecuteNonQuery method executes a non-query statement (Update, Insert, Delete, etc.). The returned value is the number of rows affected by execution. Example: Execute the simple Insert statement using (SqlCommandcmd = conn. createCommand () {cmd. commandText = "Insert into MyTAble1 (Name) values ('abc')"; cmd. executeNonQuery (); // execute a non-query statement} (B) The ExecuteScale method is used to execute the query and return the first column of the First row in the returned result set, because the type of the returned value cannot be determined, the returned value is of the Object type. Cmd. commandText = "select count (*) from T_Users"; int I = Convert. toInt32 (cmd. executeScalar (); (c) ExecuteReader method. Execute the statement that returns a result set (multiple rows and multiple columns of data. Using (SqlDataReader reader = cmd. executeReader () {while (reader. read () {Console. writeLine (reader. getString (1) ;}} (3) SqlDataReader object to create SqlDataReader, you must call the ExecuteReader method of the SqlCommand object instead of using the constructor. Syntax: SqlDataReader sdr = cmd. ExecuteReader (); methods of SqlDataReader objects, such as GetString and GetInt32, only accept Integer Parameters, that is, serial numbers. Use the GetOrdinal method to obtain the serial numbers dynamically based on the column name. The DataReader object only provides fast transmission of read-only items. You can only read the next data in one direction. The data in the read-only DataReader is read-only and cannot be modified, data in DataSet can be read and modified at will. Example: using (SqlDataReader reader = cmd. executeReader () {while (reader. read () {// Console. writeLine (reader. getString (1); Console. writeLine (reader. getString (reader. getOrdinal ("UserName"); // reader. getOrdinal ("UserName"); // obtain the sequence number of the Column Based on the column name .}} conclusion: ADO.. Net: the connection object of the SqlConnection data source. SqlCommand object for addition, deletion, modification, query operations. SqlDataAdapter adapter object. SqlDataReader data stream reader.

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.