SqlDataSourceView.DeleteCommand Propiedad

Definición

Obtiene o establece la cadena SQL que SqlDataSourceView utiliza para eliminar datos de la base de datos subyacente.Gets or sets the SQL string that the SqlDataSourceView uses to delete data from the underlying database.

public:
 property System::String ^ DeleteCommand { System::String ^ get(); void set(System::String ^ value); };
public string DeleteCommand { get; set; }
member this.DeleteCommand : string with get, set
Public Property DeleteCommand As String

Valor de propiedad

String

Una cadena de SQL que utiliza el control SqlDataSourceView para eliminar datos.An SQL string that the SqlDataSourceView uses to delete data.

Ejemplos

En el ejemplo de código siguiente se muestra cómo establecer el DeleteCommand texto para eliminar un pedido de la tabla Orders de la base de datos Northwind.The following code example demonstrates how to set the DeleteCommand text to delete an order from the Northwind database Orders table. Los datos se recuperan de la tabla Orders y se muestran en un GridView control.Data is retrieved from the Orders table and displayed in a GridView control. El GridView representa automáticamente un botón eliminar cuando la AutoGenerateDeleteButton propiedad se establece en true y rellena automáticamente la DeleteParameters colección y llama al Delete método cuando se hace clic en el botón eliminar .The GridView renders a Delete button automatically when the AutoGenerateDeleteButton property is set to true and automatically populates the DeleteParameters collection and calls the Delete method when the Delete button is clicked. Por último, dado que en este ejemplo se eliminan datos, se agrega un controlador de eventos para intentar hacer una copia de seguridad de la base de datos en el disco antes de que se realice la operación de eliminación.Finally, because this example deletes data, an event handler is added to attempt to back up the database to disk before the delete operation is performed.

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {    
    // Cancel the delete operation if the checkbox is not checked.
    if (! CheckBox1.Checked) {
        e.Cancel = true;
        Label1.Text = "The command was cancelled because the CheckBox was not checked.";
    }
 }

private void OnRecordDeleted(object source, SqlDataSourceStatusEventArgs e) {
    Label1.Text = e.AffectedRows + " row(s) were deleted";
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"            
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
            OnDeleting="OnRecordDeleting"
            OnDeleted="OnRecordDeleted">
        </asp:SqlDataSource>
        <br />
       <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

        <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataKeyNames="OrderID"
            AutoGenerateDeleteButton="True"
            AllowPaging="True"
            PageSize="20"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
                <asp:BoundField HeaderText="Customer" DataField="CustomerID" />
                <asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
                <asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
            </Columns>
        </asp:GridView>

        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
 Sub On_Record_Deleting(ByVal source As Object, ByVal e As SqlDataSourceCommandEventArgs)
     ' Cancel the delete operation if the checkbox is not checked.
     If Not CheckBox1.Checked 
            e.Cancel = True
            Label1.Text = "The command was cancelled because the CheckBox was not checked."
     End If

End Sub 'On_Record_Deleting

Sub On_Record_Deleted(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
    Label1.Text = e.AffectedRows & " row(s) were deleted"

End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
            OnDeleting="On_Record_Deleting"
            OnDeleted="On_Record_Deleted">
        </asp:SqlDataSource>
        <br />

       <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

        <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataKeyNames="OrderID"
            AutoGenerateDeleteButton="True"
            AllowPaging="True"
            PageSize="20"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
                <asp:BoundField HeaderText="Customer" DataField="CustomerID" />
                <asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
                <asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
            </Columns>
        </asp:GridView>

        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>

    </form>
  </body>
</html>

Comentarios

Como productos de base de datos diferentes usan variedades diferentes de SQL, la sintaxis de la cadena de SQL depende del proveedor de ADO.NET actual que se está usando, identificado por la propiedad ProviderName.Because different database products use different varieties of SQL, the syntax of the SQL string depends on the current ADO.NET provider being used, which is identified by the ProviderName property.

Si la cadena de SQL es un comando o consulta parametrizado, el marcador de posición del parámetro también depende del proveedor de ADO.NET que se está usando.If the SQL string is a parameterized query or command, the placeholder of the parameter also depends on the ADO.NET provider being used. Por ejemplo, si el proveedor es System.Data.SqlClient , que es el proveedor predeterminado para la SqlDataSource clase, el marcador de posición del parámetro es '@parameterName' .For example, if the provider is the System.Data.SqlClient, which is the default provider for the SqlDataSource class, the placeholder of the parameter is '@parameterName'. Sin embargo, si el proveedor está establecido en System.Data.Odbc o System.Data.OleDb , el marcador de posición del parámetro es '?' .However, if the provider is set to the System.Data.Odbc or System.Data.OleDb, the placeholder of the parameter is '?'. Para obtener más información sobre los comandos y las consultas SQL parametrizados, vea usar parámetros con el control SqlDataSource.For more information on parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

DeleteCommandPuede ser una cadena SQL o el nombre de un procedimiento almacenado, si la base de datos subyacente admite procedimientos almacenados.The DeleteCommand can be an SQL string or the name of a stored procedure, if the underlying database supports stored procedures.

El valor de la DeleteCommand propiedad se almacena en el estado de vista.The value of the DeleteCommand property is stored in view state.

Se aplica a

Consulte también