Propriedade do RDL SqlCeDataAdapter.DeleteCommand
Obtém ou define uma instrução SQL para excluir registros do conjunto de dados.
Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (em System.Data.SqlServerCe.dll)
Sintaxe
'Declaração
Public Property DeleteCommand As SqlCeCommand
Get
Set
'Uso
Dim instance As SqlCeDataAdapter
Dim value As SqlCeCommand
value = instance.DeleteCommand
instance.DeleteCommand = value
public SqlCeCommand DeleteCommand { get; set; }
public:
property SqlCeCommand^ DeleteCommand {
SqlCeCommand^ get ();
void set (SqlCeCommand^ value);
}
member DeleteCommand : SqlCeCommand with get, set
function get DeleteCommand () : SqlCeCommand
function set DeleteCommand (value : SqlCeCommand)
Valor da propriedade
Tipo: System.Data.SqlServerCe.SqlCeCommand
Um SqlCeCommand usado durante a Update para excluir registros da fonte de dados que correspondem às linhas excluídas do DataSet.
Comentários
Durante uma chamada para Update, se esta propriedade não estiver definida e as informações da chave primária estiverem presentes no DataSet, DeleteCommand poderá ser gerado automaticamente, se você definir a propriedade SelectCommand e usar o SqlCeCommandBuilder. Então, todos os comandos adicionais que não forem definidos serão gerados pelo SqlCeCommandBuilder. Essa lógica de geração exige que as informações da coluna de chave estejam presentes no DataSet.
Quando DeleteCommand for atribuído a um SqlCeCommand criado anteriormente, o SqlCeCommand não será clonado. O DeleteCommand mantém uma referência ao objeto SqlCeCommand criado anteriormente.
Exemplos
O exemplo a seguir cria um SqlCeDataAdapter e define algumas de suas propriedades.
Dim cmd As SqlCeCommand = Nothing
Dim adp As SqlCeDataAdapter = Nothing
Try
adp = New SqlCeDataAdapter()
Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf")
' Create the SelectCommand
'
cmd = conn.CreateCommand()
cmd.CommandText = "SELECT * FROM Orders WHERE [Ship Country] = @country AND [Ship City] = @city"
cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15)
cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15)
cmd.Parameters("@country").Value = "UK"
cmd.Parameters("@city").Value = "London"
adp.SelectCommand = cmd
' Create the DeleteCommand
'
cmd = conn.CreateCommand()
cmd.CommandText = "DELETE FROM Orders WHERE [Order ID] = @orderID"
Dim p As SqlCeParameter = cmd.Parameters.Add("@orderID", SqlDbType.NChar, 5, "Order ID")
p.SourceVersion = DataRowVersion.Original
adp.DeleteCommand = cmd
' Populate the dataset with the results from the SELECT statement
'
Dim ds As New DataSet()
adp.Fill(ds)
' Modify the dataset
'
MessageBox.Show("Number of rows: " & ds.Tables(0).Rows.Count)
' Delete some rows
'
ds.Tables(0).Rows(3).Delete()
ds.Tables(0).Rows(4).Delete()
' This will execute two DELETE statements
'
adp.Update(ds.Tables(0))
Catch e As Exception
MessageBox.Show(e.Message)
Finally
If Not Nothing Is adp.SelectCommand Then
adp.SelectCommand.Dispose()
End If
If Not Nothing Is adp.DeleteCommand Then
adp.DeleteCommand.Dispose()
End If
End Try
SqlCeCommand cmd = null;
SqlCeDataAdapter adp = null;
try
{
adp = new SqlCeDataAdapter();
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf");
// Create the SelectCommand
//
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM Orders WHERE [Ship Country] = @country AND [Ship City] = @city";
cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15);
cmd.Parameters["@country"].Value = "UK";
cmd.Parameters["@city"].Value = "London";
adp.SelectCommand = cmd;
// Create the DeleteCommand
//
cmd = conn.CreateCommand();
cmd.CommandText = "DELETE FROM Orders WHERE [Order ID] = @orderID";
SqlCeParameter p = cmd.Parameters.Add("@orderID", SqlDbType.NChar, 5, "Order ID");
p.SourceVersion = DataRowVersion.Original;
adp.DeleteCommand = cmd;
// Populate the dataset with the results from the SELECT statement
//
DataSet ds = new DataSet();
adp.Fill(ds);
// Modify the dataset
//
MessageBox.Show("Number of rows: " + ds.Tables[0].Rows.Count);
// Delete some rows
//
ds.Tables[0].Rows[3].Delete();
ds.Tables[0].Rows[4].Delete();
// This will execute two DELETE statements
//
adp.Update(ds.Tables[0]);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (null != adp.SelectCommand) adp.SelectCommand.Dispose();
if (null != adp.DeleteCommand) adp.DeleteCommand.Dispose();
}