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 }