ServerConnection.ExecuteNonQuery-Methode (String)
Führt eine Anweisung aus, die kein Resultset zurückgibt.
Namespace: Microsoft.SqlServer.Management.Common
Assembly: Microsoft.SqlServer.ConnectionInfo (in Microsoft.SqlServer.ConnectionInfo.dll)
Syntax
'Declaration
Public Function ExecuteNonQuery ( _
sqlCommand As String _
) As Integer
'Usage
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
Parameter
- sqlCommand
Typ: String
Ein String-Wert, der den auszuführenden Transact-SQL-Befehl angibt.
Rückgabewert
Typ: Int32
Ein Int32-Wert, der die Gesamtanzahl der Zeilen angibt, die vom Transact-SQL-Befehl für UPDATE-, INSERT- und DELETE-Anweisungen betroffen sind.Für alle anderen Anweisungstypen beträgt der Rückgabewert -1.
Hinweise
Der Befehl Transact-SQL ist normalerweise eine Anweisung Datendefinitionssprachen (ddl)- oder eine gespeicherte Prozedur, die die Servereinstellungen auswirkt. Die Text-Eigenschaft des CapturedSql-Objekts kann sich vom sqlCommand-Parameter verwendet werden, um die Ausführung einer Anweisung erfassten Transact-SQL zuzulassen. Der sqlCommand-Parameter enthält eine einzelne Transact-SQL-Anweisung.
Nur SQLCMD-Befehle werden durch die ExecuteNonQuery-Methode erkannt. Wenn der sqlCommand-Parameter eine Anweisung, die kein SQLCMD-Befehl dann die Methode ist, enthält ExecutionFailureException fehlschlägt und eine Ausnahme wird ausgelöst.
Beispiele
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
Siehe auch
Verweis
Microsoft.SqlServer.Management.Common-Namespace