SqlDataSource.DeleteCommand Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene o imposta la stringa SQL utilizzata dal SqlDataSource controllo per eliminare i dati dal database sottostante.
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
Valore della proprietà
Stringa SQL utilizzata SqlDataSource da per eliminare i dati.
Esempio
Nell'esempio di codice seguente viene illustrato come impostare il DeleteCommand testo per eliminare un ordine dalla tabella Ordini di database Northwind. I dati vengono recuperati dalla tabella Orders e visualizzati in un GridView controllo . Esegue GridView automaticamente il rendering di un pulsante Elimina quando la AutoGenerateDeleteButton proprietà è impostata su true. Inoltre, quando si fa clic sul pulsante Elimina , il GridView controllo popola automaticamente la DeleteParameters raccolta e chiama il Delete metodo . Infine, poiché questo esempio di codice elimina i dati, viene aggiunto un gestore eventi per tentare di eseguire il backup del database su disco prima dell'esecuzione dell'operazione Delete .
<%@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>
Commenti
DeleteCommand Rappresenta una query SQL o il nome di una stored procedure e viene utilizzata dal Delete metodo .
Poiché i diversi prodotti di database usano diverse varietà di SQL, la sintassi della stringa SQL dipende dal provider di ADO.NET corrente usato, identificato dalla ProviderName proprietà . Se la stringa SQL è una query o un comando con parametri, la sintassi del parametro dipende anche dal provider ADO.NET in uso. Ad esempio, se il provider è , System.Data.SqlClientche è il provider predefinito per la SqlDataSource classe , la sintassi del parametro è '@parameterName'. Tuttavia, se il provider è impostato su System.Data.Odbc o System.Data.OleDb, il segnaposto del parametro è '?'. Per altre informazioni sulle query e i comandi SQL con parametri, vedere Uso di parametri con il controllo SqlDataSource.
La DeleteCommand proprietà può essere una stringa SQL o il nome di una stored procedure, se il database supporta stored procedure.
La DeleteCommand proprietà delega alla DeleteCommand proprietà dell'oggetto SqlDataSourceView associato al SqlDataSource controllo .
Importante
Ai fini della sicurezza, la DeleteCommand proprietà non viene archiviata nello stato di visualizzazione. Poiché è possibile decodificare il contenuto dello stato di visualizzazione nel client, l'archiviazione di informazioni riservate sulla struttura del database nello stato di visualizzazione potrebbe causare una vulnerabilità di divulgazione di informazioni.