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>

注解

可以在 控件将 SelectedUpdated、 、 InsertedDeleted 事件提交SqlDataSource到数据库后对其进行检查和操作DbCommand。 属性Command使你能够在通过数据库ParametersCommandText属性以及属性(表示提交到数据库的 SQL 查询、命令或存储过程名称)执行数据库操作后访问任何输出参数中的返回值和值。

任何输出参数都专门来自具有 InputOutput 对象的 属性的 或 OutputDirection 的参数 Parameter 。 返回值来自具有 ReturnValue 值的参数。

适用于

另请参阅