Execute SQL statement: Command object

Command object overview

The Command object is a data command object whose main function is to send SQL statements for query, update, delete, and modify operations to the database. The Command object mainly has the following methods.

SqlCommand : Used to send SQL statements to the SQL Server database, located in the System.Data.SqlClient namespace.

OleDbCommand : Used to send SQL statements to the database exposed using OLEDB, located in the System.Data.OleDb namespace. For example, Access database and MySQL database are both OLEDB public databases.

OdbcCommand : Used to send SQL statements to the database exposed by ODBC, located in the System.Data.Odbc namespace. Some databases do not provide the corresponding connection program, you can use OdbcCommand after configuring the ODBC connection.

OracleCommand : Used to send SQL statements to the Oracle database, located in the System.Data.OracleClient namespace.

 

Set data source type

     The Command object has three important properties, namely Connection property, CommandText property and CommandType property. The Connection property is used to set the SqlConnection used by SqlCommand. The CommandText property is used to set the SQL statement or stored procedure to be executed on the data source. The CommandType property is used to set the type of the specified CommandText. The value of the CommandType property is the CommandType enumeration value. The CommandType enumeration has 3 enumeration members, which are introduced as follows.

StoredProcedure : The name of the stored procedure.

TableDirect : The name of the table.

Text : SQL text command.

 

Execute SQL statement

       The Command object needs to obtain the SQL statement to be executed, and submit the SQL statement to the database by calling the various methods provided by this class.

     (1) ExecuteNonQuery method: used to execute non-Select commands, usually executing SQL statements that change the database.

       It is usually used to execute Update, Insert and Delete commands, and return the number of data rows affected by the three commands; it can also be used to execute some data definition commands, such as creating, updating, and deleting database objects (such as tables, indexes, etc.)

       The meaning of the return value of this method is as follows:

  For Update, Insert, and Delete statements, the return value is the number of rows affected by the command.

  For all other types of statements, the return value is -1.

       The Command object updates the database through the ExecuteNonQuery method. The steps required are as follows:

       ① Create a database connection.

       ② Create a Command object, and specify a SQL Insert, Update, Delete query or stored procedure.

       ③Attach the Command object to the database connection.

       ④ Call the ExecuteNonQuery method.

       ⑤Close the connection.

 

     (2) ExecuteReader method: mainly used to read records in the database. Execute the Select command to return a DataReader object, this DataReader object is a read-only forward data set.

 

     (3) ExecuteScalar method: used to execute the Select query command and return the value of the first row and first column in the data. This method is usually used to execute the Select command that uses the COUNT or SUM function.

Guess you like

Origin blog.csdn.net/weixin_44684272/article/details/105481315