ServerConnection.ExecuteNonQuery 方法 (String)

执行不返回结果集的一个语句。

命名空间:  Microsoft.SqlServer.Management.Common
程序集:  Microsoft.SqlServer.ConnectionInfo(在 Microsoft.SqlServer.ConnectionInfo.dll 中)

语法

声明
Public Function ExecuteNonQuery ( _
    sqlCommand As String _
) As Integer
用法
Dim instance As ServerConnection 
Dim sqlCommand As String 
Dim returnValue As Integer 

returnValue = instance.ExecuteNonQuery(sqlCommand)
public int ExecuteNonQuery(
    string sqlCommand
)
public:
int ExecuteNonQuery(
    String^ sqlCommand
)
member ExecuteNonQuery : 
        sqlCommand:string -> int
public function ExecuteNonQuery(
    sqlCommand : String
) : int

参数

  • sqlCommand
    类型:String
    一个 String 值,该值指定要执行的 Transact-SQL 命令。

返回值

类型:Int32
一个 Int32 值,该值指定 UPDATE、INSERT 和 DELETE 语句的 Transact-SQL 命令影响的总行数。对于所有其他类型的语句,返回值是 -1

注释

Transact-SQL 命令通常会影响服务器设置的数据定义语言 (DDL) (DDL) 声明或存储过程。 CapturedSql 对象的 Text 物产可作为 sqlCommand 参数可以是根据夺取的 Transact-SQL 语句的执行。 sqlCommand 参数包含一个 Transact-SQL 语句。

SQLCMD 命令由 ExecuteNonQuery 方法仅识别。 如果 sqlCommand 参数包含一个未声明一个 SQLCMD 命令方法会失败 , 并引发 ExecutionFailureException 异常。

示例

VB

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Set the execution mode to CaptureSql for the connection.
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql
'Make a modification to the server that is to be captured.
srv.UserOptions.AnsiNulls = True
srv.Alter()
'Iterate through the strings in the capture buffer and display the captured statements.
Dim s As String
For Each s In srv.ConnectionContext.CapturedSql.Text
    Console.WriteLine(s)
Next
'Execute the captured statements.
srv.ConnectionContext.ExecuteNonQuery(srv.ConnectionContext.CapturedSql.Text)
'Revert to immediate execution mode. 
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql

PowerShell

$srv = new-object Microsoft.SqlServer.Management.Smo.Server
$srv.ConnectionContext.SqlExecutionModes = [Microsoft.SqlServer.Management.Common.SqlExecutionModes]::CaptureSql
$srv.UserOptions.AnsiNulls = $TRUE
$srv.Alter()
foreach ($s in $srv.ConnectionContext.CapturedSql.Text)
{
   Write-Host $s
}
$srv.ConnectionContext.ExecuteNonQuery($srv.ConnectionContext.CapturedSql.Text)
$srv.ConnectionContext.SqlExecutionModes = [Microsoft.SqlServer.Management.Common.SqlExecutionModes]::ExecuteSql

请参阅

参考

ServerConnection 类

ExecuteNonQuery 重载

Microsoft.SqlServer.Management.Common 命名空间

其他资源

使用捕获模式