针对 Connection 执行 SQL 语句并返回受影响的行数。Executes an SQL statement against the Connection and returns the number of rows affected.
public:
virtual int ExecuteNonQuery();
public:
override int ExecuteNonQuery();
public int ExecuteNonQuery ();
public override int ExecuteNonQuery ();
abstract member ExecuteNonQuery : unit -> int
override this.ExecuteNonQuery : unit -> int
override this.ExecuteNonQuery : unit -> int
Public Function ExecuteNonQuery () As Integer
Public Overrides Function ExecuteNonQuery () As Integer
返回
对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 对于 CREATE TABLE 和 DROP TABLE 语句,返回值为 0。For CREATE TABLE and DROP TABLE statements, the return value is 0. 对于其他所有类型的语句,返回值为 -1。For all other types of statements, the return value is -1.
实现
例外
连接不存在。The connection does not exist.
或-or-
连接未打开。The connection is not open.
示例
The following example creates an OracleCommand and then executes it by using ExecuteNonQuery. 该示例传递了一个字符串,该字符串是 SQL 语句 (如 UPDATE、INSERT 或 DELETE) 以及用于连接数据库的字符串。The example is passed a string that is an SQL statement (such as UPDATE, INSERT, or DELETE) and a string to use to connect to the database.
public void CreateOracleCommand(string myExecuteQuery, string connectionString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
OracleCommand command = new OracleCommand(myExecuteQuery, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}Public Sub CreateOracleCommand(ByVal myExecuteQuery As String, _
ByVal connectionString As String)
Using connection As New OracleConnection(connectionString)
Dim command As New OracleCommand(myExecuteQuery, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
注解
您可以使用 ExecuteNonQuery 执行目录操作 (例如,查询数据库的结构或创建) 的表的数据库对象; 或者, DataSet 通过执行 UPDATE、INSERT 或 DELETE 语句来更改数据库中的数据,而无需使用。You can use ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables); or to change the data in a database, without using a DataSet, by executing UPDATE, INSERT, or DELETE statements.
尽管不 ExecuteNonQuery 会返回任何行,但映射到参数的任何输出参数或返回值都将用数据填充。Although ExecuteNonQuery does not return any rows, any output parameters or return values mapped to parameters are populated with data.
对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. 对于 CREATE TABLE 和 DROP TABLE 语句,返回值为 0。For CREATE TABLE and DROP TABLE statements, the return value is 0. 对于其他所有类型的语句,返回值为 -1。For all other types of statements, the return value is -1. 如果发生回滚,则返回值也为-1。If a rollback occurs, the return value is also -1.
适用于