Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataContext Class
 ExecuteCommand Method
.NET Framework Class Library
DataContext..::.ExecuteCommand Method

Updated: November 2007

Executes SQL commands directly on the database.

Namespace:  System.Data.Linq
Assembly:  System.Data.Linq (in System.Data.Linq.dll)

Visual Basic (Declaration)
Public Function ExecuteCommand ( _
    command As String, _
    ParamArray parameters As Object() _
) As Integer
Visual Basic (Usage)
Dim instance As DataContext
Dim command As String
Dim parameters As Object()
Dim returnValue As Integer

returnValue = instance.ExecuteCommand(command, _
    parameters)
C#
public int ExecuteCommand(
    string command,
    params Object[] parameters
)
Visual C++
public:
int ExecuteCommand(
    String^ command, 
    ... array<Object^>^ parameters
)
J#
public int ExecuteCommand(
    String command,
    Object[] parameters
)
JScript
public function ExecuteCommand(
    command : String, 
    ... parameters : Object[]
) : int

Parameters

command
Type: System..::.String

The SQL command to be executed.

parameters
Type: array<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 1. The parameter is associated with the equally numbered object in the parameters array.

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

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

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

Windows Vista, Windows XP SP2, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Known bug      HowardRichards   |   Edit   |  
There is a known bug with ExecuteCommand, where null parameter values are not handled correctly.

see
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=305114

Tags What's this?: bug (x) Add a tag
Flag as ContentBug
Parameters      Peter Herschel   |   Edit   |  

In C# enumerate parameters from 0. Ex: where PostDate > {0}

Don't use SqlParameters, rather regular types like DateTime, etc...

Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker