Four methods of SqlCommand

Source: Internet
Author: User

Methods of the SqlCommand class

----"" 1.ExecuteNonQuery ();
It has a return value of type int. Many are used to perform an increase, delete, modify data, and return the number of rows affected. When the select operation, returns-1.
ExecuteNonQuery () method The primary user updates the data, usually it uses the Update,insert,delete statement to manipulate the database.

Its method returns the value meaning: For Update,insert,delete statement execution success is the number of rows affected by the return value for the command, if the number of rows affected is 0 o'clock, the value returned is 0,


----"" 2.ExecuteReader ();
It has a return type of SqlDataReader. This method is used for query operations performed by the user. The read () method of the SqlDataReader object is used for progressive reading.
The time in which the data column is read. In addition to using the reder["column name"]. ToString (); You can also use the reder[index]. Tosting ();< Note: The index here refers to the index of the column in the database. Starting from 0. >


----"" 3.ExecuteScaler ();
Its return value type is a multi-bit int type. It returns many for the execution of a select query. The resulting return result is a value, such as using the Count function to find the number of records in a table or to sum with the SUM function.
The ExecuteScalar () method is also used to execute the SQL statement, but the return value after ExecuteScalar () executes the SQL statement is not the same as ExecuteNonQuery (), ExecuteScalar () The data type of the return value of the method is the object type. If the executed SQL statement is a query statement (SELECT), the result is the first column of the first row after the query, and if the executed SQL statement is not a query statement, an object that is not instantiated is returned and must be displayed by type conversion, as shown in the sample code.
String str = "server= ' (local) ';d atabase= ' mytable '; uid= ' sa ';p wd= ' sa '"; Create a connection string
SqlConnection con = new SqlConnection (str); Create a Connection object
Con. Open (); Open connection
String strSQL = "SELECT * from Mynews ORDER by id DESC";
SqlCommand cmd = new SqlCommand (strSQL, con);
Label1.Text = "Query out ID is" + cmd. ExecuteScalar () \; Using ExecuteScalar queries
Typically, a value is returned after the ExecuteNonQuery () operation, and an object is returned after the ExecuteScalar () operation, and ExecuteScalar () is often used when a single value needs to be returned. For example, when inserting a piece of data, you often need to know immediately what value you just inserted, you can use the ExecuteScalar () method. The sample code is shown below.
String str = "server= ' (local) ';d atabase= ' mytable '; uid= ' sa ';p wd= ' sa '"; Create a connection string
SqlConnection con = new SqlConnection (str); Create a Connection object
Con. Open (); Open connection
string strSQL = "INSERT into mynews values (' What is the ID just inserted? ')
SELECT @ @IDENTITY as ' bh ';//INSERT statement
SqlCommand cmd = new SqlCommand (strSQL, con);//EXECUTE statement
Label1.Text = "The ID of the row you just inserted is" + cmd. ExecuteScalar (); Return Assignment
The code above uses the SELECT @ @IDENTITY syntax to get the ID value just after the update was performed, and then gets the value of the first column of the first row after the update by using the ExecuteScalar () method.

Four methods of SqlCommand

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.