Sqlcommand. executenonquery Method

Source: Internet
Author: User

Sqlcommand. executenonquery Method

Execute a Transact-SQL statement on the connection and return the affected number of rows, which is an int data type.

Namespace:System. Data. sqlclient
Assembly:System. Data (in system. Data. dll
Medium)

Syntax
C #
public override int ExecuteNonQuery ()
C ++
public:virtual int ExecuteNonQuery () override
J #
public int ExecuteNonQuery ()
JScript
public override function ExecuteNonQuery () : int

Return Value

Number of affected rows.

Exception

Exception type Condition

Sqlexception

An exception occurred while executing the command on the locked line. If Microsoft. NET Framework 1.0 is used, this exception is not generated.

Remarks

You can useExecutenonqueryTo perform directory operations (such as querying the database structure or creating database objects such as tables), or
Update, insert, or delete statements without using Dataset
To change the data in the database.

AlthoughExecutenonqueryNo rows are returned, but any output parameters or return values mapped to parameters are filled with 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. If rollback occurs, the returned value is
-1.

Example

The following example creates a sqlcommand and then uses
ExecutenonqueryRun it. In this example, two strings are passed: A string is a Transact-SQL statement (such as update, insert, or
The other string is used to connect to the data source.

C #
Copy code
Private Static void createcommand (string querystring, string connectionstring) {using (sqlconnection connection = new sqlconnection (connectionstring) {sqlcommand command = new

 

 

Sqlcommand (querystring, connection); command. Connection. open (); command. executenonquery ();}

 

}

Encapsulation:


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.