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

定義

直接在資料庫上執行 SQL 命令。

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 命令。

parameters
Object[]

要傳遞至命令的參數陣列。 請注意下列情況: 如果陣列中的物件數目少於命令字串中所識別的最高數目,便會擲回例外狀況。

如果陣列包含命令字串中未參考的物件,則不會擲回任何例外狀況。

如果有任一參數為 null,則會轉換成 DBNull.Value

傳回

Int32

已執行的命令所修改的資料列數。

範例

下列範例會開啟連線,並將 SQL UPDATE 命令傳遞至SQL引擎。

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

備註

此方法是一種傳遞機制,適用于LINQ to SQL未適當地針對特定案例提供的情況。

命令的語法幾乎與用來建立 ADO.NET DataCommand 的語法相同。 唯一的差異在於如何指定參數。 具體來說,您可以用大括弧括住參數, ({...}) 並從 0 開始加以列舉。 參數與參數陣列中相同編號的物件相關聯。

ExecuteQueryExecuteCommand 可讓您指定參數替代的引數數目可變。 例如,您可以在叫用 ExecuteQuery <TResult> 時指定參數:

db.ExecuteQuery<Customer>("SELECT * FROM dbo.Customers WHERE City = {0}", "London");
db.ExecuteQuery(Of Customer)("SELECT * FROM dbo.Customers WHERE City = {0}", "London")

此外,另一個範例:

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

適用於