SqlDataSourceStatusEventArgs.Command プロパティ

定義

データベースに送信されたデータベース コマンドを取得します。

public:
 property System::Data::Common::DbCommand ^ Command { System::Data::Common::DbCommand ^ get(); };
public System.Data.Common.DbCommand Command { get; }
member this.Command : System.Data.Common.DbCommand
Public ReadOnly Property Command As DbCommand

プロパティ値

データベースに送信されたデータベース コマンドを表す DbCommand オブジェクト。

次のコード例では、ストアド プロシージャでコントロールを使用するときに、出力パラメーターの値を SqlDataSource 調べる方法を示します。 コレクションには SelectParameters 、 がストアド プロシージャに使用する SqlDataSource パラメーターが含まれており、Web フォームからストアド プロシージャに情報を渡すパラメーターと、フォームに情報を返すパラメーターで構成されます。 このコード例は、SqlDataSourceStatusEventArgs クラスのために提供されている大規模な例の一部です。

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<%@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">
// Clicking the Submit button explicitly refreshes the data 
// by calling the Select() method.
private void Submit(Object source, EventArgs e) {
  SqlDataSource1.Select(DataSourceSelectArguments.Empty);
}

// This event handler is called after the Select() method is executed.
private void OnSelectedHandler(Object source, SqlDataSourceStatusEventArgs e) {

  IDbCommand cmd = e.Command; 
  
  Label1.Text = "Parameter return values: ";

  foreach (SqlParameter param in cmd.Parameters) {
    //  Extract the value of the parameter.
    Label1.Text += param.ParameterName + " - " + param.Value.ToString();
  }
}
</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="getordertotal"
            onselected="OnSelectedHandler">
            <selectparameters>
              <asp:querystringparameter name="empId" querystringfield="empId" />
              <asp:parameter name="total" type="Int32" direction="Output" defaultvalue="0" />
              <asp:parameter name="_ret" type="Int32" direction="ReturnValue" defaultvalue="0" />
            </selectparameters>
        </asp:sqldatasource>
        <!--
          CREATE PROCEDURE dbo.getordertotal
            @empId int,
            @total int OUTPUT
          as
            set nocount on
            select @total    = count(1) from orders where employeeid=@empid;
            select * from orders where employeeID = @empId ;
            return (-1000);
          GO
        -->

        <asp:gridview
          id="GridView1"
          runat="server"
          allowpaging="True"
          pagesize="5"
          datasourceid="SqlDataSource1" />

        <asp:button
          id="Button1"
          runat="server"
          onclick="Submit"
          text="Refresh Data" />

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

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<%@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">
' Clicking the Submit button explicitly refreshes the data 
' by calling the Select() method.
Private Sub Submit(source As Object, e As EventArgs)
  
  SqlDataSource1.Select(DataSourceSelectArguments.Empty)
  
End Sub ' Submit

' This event handler is called after the Select() method is executed.
Private Sub OnSelectedHandler(source As Object, e As SqlDataSourceStatusEventArgs)

  Dim cmd As IDbCommand 
  cmd = e.Command
  Dim param As SqlParameter
  
  Label1.Text = "Parameter return values: "
  
  For Each param In cmd.Parameters
    
    ' Extract the name and value of the parameter.
    Label1.Text = Label1.Text & param.ParameterName & " - " & _
                  param.Value.ToString()

  Next

End Sub ' OnSelectedHandler
</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="getordertotal"
            onselected="OnSelectedHandler">
            <selectparameters>
              <asp:querystringparameter name="empId" querystringfield="empId" />
              <asp:parameter name="total" type="Int32" direction="Output" defaultvalue="0" />
              <asp:parameter name="_ret" type="Int32" direction="ReturnValue" defaultvalue="0" />
            </selectparameters>
        </asp:sqldatasource>
        <!--
          CREATE PROCEDURE dbo.getordertotal
            @empId int,
            @total int OUTPUT
          as
            set nocount on
            select @total    = count(1) from orders where employeeid=@empid;
            select * from orders where employeeID = @empId ;
            return (-1000);
          GO
        -->

        <asp:gridview
          id="GridView1"
          runat="server"
          allowpaging="True"
          pagesize="5"
          datasourceid="SqlDataSource1" />

        <asp:button
          id="Button1"
          runat="server"
          onclick="Submit"
          text="Refresh Data" />

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

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

注釈

、または Deleted イベントをSelectedUpdatedInserted処理して、 コントロールによってデータベースに送信された後、 のDbCommandプロパティをSqlDataSource調べて操作できます。 Commandプロパティを使用すると、データベース操作がプロパティを使用してParameters実行された後、出力パラメーターの戻り値と値、およびCommandTextデータベースに送信された SQL クエリ、コマンド、またはストアド プロシージャ名を表す プロパティにアクセスできます。

出力パラメーターは、特に、 オブジェクトの プロパティの または 値をDirection持つInputOutputパラメーターから取得されますParameterOutput 戻り値は、値を持つパラメーターからの値です ReturnValue

適用対象

こちらもご覧ください