Επεξεργασία

Κοινή χρήση μέσω


DataContext.ExecuteCommand(String, Object[]) Method

Definition

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

Parameters

command
String

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.

If any one of the parameters is null, it is converted to DBNull.Value.

Returns

The number of rows modified by the executed command.

Examples

The following example opens a connection and passes a SQL UPDATE command to the SQL engine.

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

Remarks

This method is a pass-through mechanism for cases where LINQ to SQL does not adequately provide for a particular scenario.

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. 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.

ExecuteQuery and ExecuteCommand allow you to specify a variable number of arguments for parameter substitution. For example, you can specify the parameters when invoking 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")

And, another example:

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)

Applies to