DataContext.ExecuteCommand(String, Object[]) Método
Definição
Executa comandos SQL diretamente no banco de dados.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
Parâmetros
- command
- String
O comando SQL a ser executado.The SQL command to be executed.
- parameters
- Object[]
A matriz de parâmetros a serem passados para o comando.The array of parameters to be passed to the command. Observe o seguinte comportamento:Note the following behavior: Se o número de objetos na matriz for menor que o maior número identificado na cadeia de caracteres de comando, uma exceção será gerada.If the number of objects in the array is less than the highest number identified in the command string, an exception is thrown.
Se a matriz contiver objetos que não são referenciados na cadeia de caracteres de comando, nenhuma exceção será gerada.If the array contains objects that are not referenced in the command string, no exception is thrown.
Se um dos parâmetros for nulo, ele será convertido em DBNull.Value.If any one of the parameters is null, it is converted to DBNull.Value.
Retornos
O número de linhas modificadas pelo comando executado.The number of rows modified by the executed command.
Exemplos
O exemplo a seguir abre uma conexão e passa um UPDATE comando SQL para o mecanismo SQL.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")
Comentários
Esse método é um mecanismo de passagem para casos em que LINQ to SQL não fornece adequadamente um cenário específico.This method is a pass-through mechanism for cases where LINQ to SQL does not adequately provide for a particular scenario.
A sintaxe para o comando é quase igual à sintaxe usada para criar um ADO.NET DataCommand .The syntax for the command is almost the same as the syntax used to create an ADO.NET DataCommand. A única diferença é em como os parâmetros são especificados.The only difference is in how the parameters are specified. Especificamente, você especifica parâmetros ao colocá-los entre chaves ({...}) e enumera-los a partir de 0.Specifically, you specify parameters by enclosing them in braces ({…}) and enumerate them starting from 0. O parâmetro é associado ao objeto igualmente numerado na matriz de parâmetros.The parameter is associated with the equally numbered object in the parameters array.
ExecuteQuery e ExecuteCommand permitem que você especifique um número variável de argumentos para substituição de parâmetro.ExecuteQuery and ExecuteCommand allow you to specify a variable number of arguments for parameter substitution. Por exemplo, você pode especificar os parâmetros ao invocar ExecuteQuery <TResult> :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")
E outro exemplo: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)