Sqlcommand. Execute three methods required for ADO. net

Source: Internet
Author: User

Sqlcommand. executenonqueryMethod

 

Execute the transact-SQL statement on the connection and return the affected number of rows.

-Syntax:

Public override int executenonquery ()

-- Return Value

Type: system...:. int32

 

Number of affected rows.

 

-Executenonquery can be used to perform directory operations (such as querying the database structure or creating database objects such as tables), or by executing update, insert, or delete statements, change the data in the database without using dataset.

 

Although executenonquery does not return any rows, 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. If a trigger exists on the table that is executing the insert or update operation, the returned value includes the number of rows affected by the insert or update operation and the number of rows affected by one or more triggers. For all other types of statements, the return value is-1. If a rollback occurs, the returned value is-1.

 

Sqlcommand. executescalarMethod

Execute the query and return the first column in the first row of the returned result set. Ignore other columns or rows.

-Syntax:

Public override object executescalar ()

 

-- Return Value

Type: system. Object

The first column in the first row of the result set. If the result set is empty, it is a null reference (nothing in Visual Basic ).

 

-Use the executescalar method to retrieve a single value (for example, an aggregate value) from the database ). This operation requiresCodeRelatively small.

 

-Typical executescalar queries can be in a format similar to the following C # example:

Cmd. commandtext = "select count (*) from DBO. region ";

Int32 COUNT = (int32) cmd. executescalar ();

 

Sqlcommand. executereaderMethod

Send commandtext to connection and generate a sqldatareader.

 

-Syntax:

Public sqldatareader executereader ()

 

-- Return Value

Type: system. Data. sqlclient...:. sqldatareader

 

A sqldatareader object.

 

-- Example:

The following example creates a sqlcommand and then runs it by passing two strings. One string is the transact-SQL SELECT statement, and the other string is used to connect to the data source.

 1   Private   Static   Void Createcommand ( String  Querystring,  2       String  Connectionstring) 3   {  4       Using (Sqlconnection connection = New  Sqlconnection (  5   Connectionstring ))  6   {  7   Connection. open ();  8   9 Sqlcommand command = New Sqlcommand (querystring, connection );  10 Sqldatareader reader = Command. executereader ();  11           While  (Reader. Read ())  12   {  13 Console. writeline (string. Format ( "  {0}  " , Reader [ 0  ]); 14   }  15   }  16 }

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.