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[]

명령에 전달할 매개 변수 배열입니다. 다음 동작에 유의하십시오.

배열의 개체 수가 명령 문자열에 지정된 최대 개수보다 작으면 예외가 throw됩니다.

명령 문자열에서 참조되지 않은 개체가 배열에 있는 경우에는 예외가 throw되지 않습니다.

null인 매개 변수는 DBNull.Value로 변환됩니다.

반환

명령을 실행하여 수정된 행 수입니다.

예제

다음 예제에서는 연결을 열고 SQL 엔진에 SQL UPDATE 명령을 전달합니다.

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부터 열거합니다. 매개 변수는 매개 변수 배열의 번호가 같은 개체와 연결됩니다.

ExecuteQuery 매개 ExecuteCommand 변수 대체에 대한 가변 수의 인수를 지정할 수 있습니다. 예를 들어 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)

적용 대상