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 值,指定受到 INSERT、UPDATE 和 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
請參閱
參考
Microsoft.SqlServer.Management.Common 命名空間