SqlCommand.ExecuteNonQuery SqlCommand.ExecuteNonQuery SqlCommand.ExecuteNonQuery SqlCommand.ExecuteNonQuery Method

定义

对连接执行 Transact-SQL 语句并返回受影响的行数。Executes a Transact-SQL statement against the connection and returns the number of rows affected.

public:
 virtual int ExecuteNonQuery();
public int ExecuteNonQuery ();
abstract member ExecuteNonQuery : unit -> int
override this.ExecuteNonQuery : unit -> int
Public Function ExecuteNonQuery () As Integer

返回

受影响的行数。The number of rows affected.

实现

异常

Value 设置为 Stream 时,使用的是 SqlDbType 而不是 Binary 或 VarBinary。A SqlDbType other than Binary or VarBinary was used when Value was set to Stream. 有关流式处理的详细信息,请参阅 SqlClient 流支持For more information about streaming, see SqlClient Streaming Support.

- 或 --or-

Value 设置为 TextReader 时,使用的是 SqlDbType 而不是 Char、NChar、NVarChar、VarChar 或 Xml。A SqlDbType other than Char, NChar, NVarChar, VarChar, or Xml was used when Value was set to TextReader.

- 或 --or-

Value 设置为 XmlReader 时,使用的是 SqlDbType 而不是 Xml。A SqlDbType other than Xml was used when Value was set to XmlReader.

对锁定的行执行命令时出现异常。An exception occurred while executing the command against a locked row. 当使用 Microsoft .NET Framework 1.0 版时不生成此异常。This exception is not generated when you are using Microsoft .NET Framework version 1.0.

- 或 --or-

流式处理操作期间发生了超时。A timeout occurred during a streaming operation. 有关流式处理的详细信息,请参阅 SqlClient 流支持For more information about streaming, see SqlClient Streaming Support.

流式处理操作期间在 StreamXmlReaderTextReader 对象中出现了错误。An error occurred in a Stream, XmlReader or TextReader object during a streaming operation. 有关流式处理的详细信息,请参阅 SqlClient 流支持For more information about streaming, see SqlClient Streaming Support.

流式处理操作期间关闭或删除了 SqlConnectionThe SqlConnection closed or dropped during a streaming operation. 有关流式处理的详细信息,请参阅 SqlClient 流支持For more information about streaming, see SqlClient Streaming Support.

流式处理操作期间关闭了 StreamXmlReaderTextReader 对象。The Stream, XmlReader or TextReader object was closed during a streaming operation. 有关流式处理的详细信息,请参阅 SqlClient 流支持For more information about streaming, see SqlClient Streaming Support.

示例

下面的示例创建一个SqlCommand ,然后使用来ExecuteNonQuery执行它。The following example creates a SqlCommand and then executes it using ExecuteNonQuery. 该示例向传递了一个字符串,该字符串是 Transact-sql 语句(如 UPDATE、INSERT 或 DELETE)以及用来连接到数据源的字符串。The example is passed a string that is a Transact-SQL statement (such as UPDATE, INSERT, or DELETE) and a string to use to connect to the data source.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using
End Sub

注解

您可以使用ExecuteNonQuery执行目录操作(例如,查询数据库的结构或创建数据库对象,例如表),或者DataSet通过执行 UPDATE、INSERT 或 DELETE 来更改数据库中的数据,而无需使用。前瞻性.You can use the 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 the ExecuteNonQuery returns no 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. 对于其他所有类型的语句,返回值为 -1。For all other types of statements, the return value is -1.

如果在要插入或更新的表中存在触发器,则返回值将包含受 insert 或 update 操作影响的行数以及触发器或触发器所影响的行数。When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers.

当在连接上设置 NOCOUNT 时(在执行命令之前或作为执行命令的一部分,或作为执行命令时触发的触发器的一部分),受单个语句影响的行将停止对此 m 返回的受影响行计数方法.When SET NOCOUNT ON is set on the connection (before or as part of executing the command, or as part of a trigger initiated by the execution of the command) the rows affected by individual statements stop contributing to the count of rows affected that is returned by this method.

如果未检测到涉及计数的语句,则返回值为-1。If no statements are detected that contribute to the count, the return value is -1. 如果发生回滚,则返回值也为-1。If a rollback occurs, the return value is also -1.

适用于

另请参阅