SqlDataSourceView.DeleteCommand プロパティ
アセンブリ: System.Web (system.web.dll 内)

Dim instance As SqlDataSourceView Dim value As String value = instance.DeleteCommand instance.DeleteCommand = value
/** @property */ public String get_DeleteCommand () /** @property */ public void set_DeleteCommand (String value)
データを削除するために SqlDataSourceView が使用する SQL 文字列。

異なるデータベース製品では異なる種類の SQL が使用されるため、SQL 文字列の構文は、現在使用されている ADO.NET プロバイダによって異なります。このプロバイダは、ProviderName プロパティによって示されます。
SQL 文字列がパラメータ化されたクエリまたはコマンドである場合、パラメータのプレースホルダは、使用されている ADO.NET プロバイダに依存します。たとえば、プロバイダが SqlDataSource クラスの既定のプロバイダである System.Data.SqlClient の場合、パラメータのプレースホルダは '@parameterName' です。ただし、プロバイダが System.Data.Odbc または System.Data.OleDb に設定されている場合、パラメータのプレースホルダは '?' となります。パラメータ化された SQL クエリとコマンドの詳細については、「SqlDataSource コントロールにおけるパラメータの使用」を参照してください。
基になるデータベースがストアド プロシージャをサポートしている場合、DeleteCommand には、SQL 文字列またはストアド プロシージャの名前を指定できます。

DeleteCommand テキストを設定して、Northwind データベースの Orders テーブルから発注内容を削除する方法を次のコード例に示します。データは Orders テーブルから取得され、GridView コントロールに表示されます。GridView は、AutoGenerateDeleteButton プロパティが true に設定されている場合、自動的に [削除] ボタンを描画します。また、[削除] ボタンをクリックすると、自動的に DeleteParameters コレクションの値を設定し、Delete メソッドを呼び出します。最後に、この例ではデータを削除するため、削除操作の実行前にデータベースをディスクにバックアップしようと試みるイベント ハンドラが追加されています。
<%@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> <BODY> <FORM 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>
<%@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> <BODY> <FORM 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="VJ#" %> <%@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) { // Because this example actually deletes data from the Northwind // database, provide a way for users to recover any deleted records. SqlConnection cxn = new SqlConnection( "Data Source=localhost;Integrated Security=SSPI;" + "Initial Catalog=Northwind;Connect Timeout=15"); try { cxn.Open(); SqlCommand backup = new SqlCommand(); backup.set_Connection (cxn); backup.set_CommandText( " USE master " + " EXEC sp_addumpdevice 'disk','Northwind_1'," + "'c:\\temp\\Northwind_1.dat'" + " BACKUP DATABASE Northwind TO Northwind_1"); backup.ExecuteNonQuery(); } catch (SqlException se) { // Handle an exception thrown if the device already exists. Label1.set_Text("An error occurred while backing up your " + "database. Please check the SQL logs."); } finally { // Always release the connection. cxn.Dispose(); } Label1.set_Text("A record has been deleted. To recover your data, " + "restore the database from the " + "database backup named Northwind_1.dat located on the database " + "server in c:\\temp."); } //OnRecordDeleting </SCRIPT> <HTML> <BODY> <FORM runat="server"> <asp:SqlDataSource id="SqlDataSource1" runat="server" DataSourceMode="DataSet" ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" SelectCommand="SELECT * FROM Orders" DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;" OnDeleting="OnRecordDeleting"> </asp:SqlDataSource> <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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SqlDataSourceView.DeleteCommand プロパティのページへのリンク