DataContext.ExecuteCommand(String, Object[]) 方法

定義

直接在資料庫上執行 SQL 命令。Executes SQL commands directly on the database.

public:
 int ExecuteCommand(System::String ^ command, ... cli::array <System::Object ^> ^ parameters);
public int ExecuteCommand (string command, params object[] parameters);
member this.ExecuteCommand : string * obj[] -> int
Public Function ExecuteCommand (command As String, ParamArray parameters As Object()) As Integer

參數

command
String

要執行的 SQL 命令。The SQL command to be executed.

parameters
Object[]

要傳遞至命令的參數陣列。The array of parameters to be passed to the command. 請注意下列情況:Note the following behavior: 如果陣列中的物件數目少於命令字串中所識別的最高數目,便會擲回例外狀況。If the number of objects in the array is less than the highest number identified in the command string, an exception is thrown.

如果陣列包含命令字串中未參考的物件,則不會擲回任何例外狀況。If the array contains objects that are not referenced in the command string, no exception is thrown.

如果有任一參數為 null,則會轉換成 DBNull.ValueIf any one of the parameters is null, it is converted to DBNull.Value.

傳回

已執行的命令所修改的資料列數。The number of rows modified by the executed command.

範例

db.ExecuteCommand("UPDATE Products SET UnitPrice = UnitPrice + 1.00");
    db.ExecuteCommand _
("UPDATE Products SET UnitPrice = UnitPrice + 1.00")

備註

這個方法是一種傳遞機制,適用于未LINQ to SQLLINQ to SQL針對特定案例適當提供的情況。This method is a pass-through mechanism for cases where LINQ to SQLLINQ to SQL does not adequately provide for a particular scenario.

命令的語法幾乎與用來建立 ADO.NET DataCommand的語法相同。The syntax for the command is almost the same as the syntax used to create an ADO.NET DataCommand. 唯一的差異在於參數的指定方式。The only difference is in how the parameters are specified. 具體而言,您可以指定參數,方法是將它們括在大括弧({...})中,然後從0開始列舉它們。Specifically, you specify parameters by enclosing them in braces ({…}) and enumerate them starting from 0. 參數會與參數陣列中的同樣編號物件相關聯。The parameter is associated with the equally numbered object in the parameters array.

ExecuteQueryExecuteCommand可讓您為參數替代指定可變數目的引數。ExecuteQuery and ExecuteCommand allow you to specify a variable number of arguments for parameter substitution. 例如,您可以在叫用 ExecuteQuery<TResult > 時指定參數:For example, you can specify the parameters when invoking ExecuteQuery<TResult>:

db.ExecuteQuery<Customer>("select * from dbo.Customers where City = {0}", "London");  

還有另一個範例:And, another example:

db.ExecuteCommand("UPDATE Products SET QuantityPerUnit = {0} WHERE ProductID = {1}", "24 boxes", 5);  

下列範例會開啟連接,並將 sql UPDATE命令傳遞至 sql 引擎。The following example opens a connection and passes a SQL UPDATE command to the SQL engine.

適用於