the difference between the ExecuteNonQuery method and the ExecuteScalar method
----ExecuteNonQuery (): Executes the Command object's SQL statement, returns an int type variable, and returns the number of records affected by the operation if the SQL statement operates on records of the database, such as additions, deletions, and updates to records.
----ExecuteScalar (): Executes the Command object's SQL statement, and if the SQL statement is a select query, simply returns the 1th row 1th column in the query result set, ignoring the other rows and columns. The result returned by the method is type object, which must be cast to the desired type before it can be used. If the SQL statement is not a select query, the return result has no effect.
------"Analysis"
The ExecuteNonQuery () method of the Command object is used to perform any SQL Operations command that does not return a result set from the database, including insert INTO, UPDATE, DELETE statement, stored procedure with no value returned, CREATE DDL statements such as table and CreateIndex. The ExecuteNonQuery method can also be used to perform directory operations, such as querying the structure of a database or creating database objects such as tables. The ExecuteNonQuery method is for update, INSERT into, and delete operations statements whose return value is the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the return value is also-1.
Assuming that CMD is the command object for the SQL Server data provider (that is, SqlCommand), CN is the Connection object (keep open), and the ExecuteNonQuery method is called as shown in the following code.
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = CN;
Sets the command text for CMD, in this case to delete records with ID field values greater than 1 in the Table1 table
Cmd.commandtext = "DELETE from Table1 WHERE [id]>1";
int count = cmd. ExecuteNonQuery ();
In the code above, you create a command object cmd of type SqlCommand, set its connection as the Connection object cn,sql the command text to an SQL statement (typically add, delete, change commands). After the ExecuteNonQuery method is executed, the command text is used by the connection object to the database, and the return value count is the number of data records that are affected in the database.
The ExecuteScalar () method of a Command object is often used to execute an aggregate function because it can return only the 1th row and 1th column of the dataset. Assuming that CMD is the command object for the SQL Server data provider (that is, SqlCommand), CN is the Connection object (keep open), and the ExecuteScalar method is called as shown in the following code.
SqlCommand cmd = new SqlCommand ();
Cmd. Connection = CN;
Sets the command text for CMD, in this case, to query the number of records in the Table1 table
Cmd.commandtext = "Select COUNT (*) from Table1";
int count = (int) cmd. ExecuteScalar ();
In the code above, you create a SqlCommand type Command object cmd, set its connection as the Connection object cn,sql the command text to an SQL statement. Because the return value is of type object, a cast is required, and the count variable stores the number of records in the desired data table.
Note: If there is no data to manipulate, only the ExecuteReader method that invokes the command object is used to return a data reader (DataReader object). Because ExecuteNonQuery () and ExecuteScalar () are called when there is no data, an "object is not instantiated" error occurs. Therefore, the read () method of the data reader should be called to detect if there is any data.
Question: ExecuteNonQuery and ExecuteScalar results: Difference between ExecuteNonQuery method and ExecuteScalar method