1 out of 1 rated this helpful - Rate this topic

DataContext.ExecuteCommand Method

Executes SQL commands directly on the database.

Namespace:  System.Data.Linq
Assembly:  System.Data.Linq (in System.Data.Linq.dll)
public int ExecuteCommand(
	string command,
	params Object[] parameters
)

Parameters

command
Type: System.String
The SQL command to be executed.
parameters
Type: System.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.

Return Value

Type: System.Int32
An int representing the number of rows modified by the executed command.

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");

And, another example:

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

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");


.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Not observing the described behavior for null objects in the params list
I just wanted to echo the above poster on the issue where ExecuteCommand cannot handle null arguments. This is a very serious bug that effectively renders ExecuteCommand almost useless. Our project is currently getting around this by completely formatting all parameters ourselves as strings and passing ExecuteCommand a pure text string. This means that SQL Server must parse every single parameter, throwing away all the implicitly conveyed type information that ExecuteCommand's normal parameter passing mechanism would convey. Consequently we have horrible performance. Bummer.
Passing a DataTable paramweter to a sproc
In ADO.Net I can pass a DataTable parameter to a sproc. How can I do the same with Linq-to-Sql?
Not observing the described behavior for null objects in the params list
It says "If any one of the parameters is null, it is converted to DBNull.Value" but if I pass null I get an exception

{"A query parameter cannot be of type 'System.Object'."} System.Exception {System.NotSupportedException}

and if I explictly pass System.DBNull.Value I get a different exception

{"Unexpected type code: DBNull"} System.Exception {System.InvalidOperationException}

VS2010

Get wrong return value of ExecuteCommand
Seem my call: return this.categoryRepository.ExecuteCommand("exec sp_DeleteCategotyTree {0}", dto.CategoryID);
always return -1 value?
Stored Procedures
To execute stored procedures, make sure you prefix the command with "exec". So for example:

object[] myParams;
DataContext db;
...
db.ExecuteCommand("exec sp_my_proc {0}, {1}", myParams);