SqlDataSourceStatusEventArgs.Command Propiedad

Definición

Obtiene el comando de base de datos enviado a la base de datos.

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

Valor de propiedad

Objeto DbCommand que representa el comando de base de datos enviado a la base de datos.

Ejemplos

En el ejemplo de código siguiente se muestra cómo examinar los valores de los parámetros de salida cuando se usa un SqlDataSource control con un procedimiento almacenado. La SelectParameters colección contiene los parámetros que SqlDataSource usa para el procedimiento almacenado y consta de parámetros que pasan información del formulario web al procedimiento almacenado, así como parámetros que devuelven información al formulario. Este ejemplo de código es parte de un ejemplo más grande proporcionado para la clase 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>

Comentarios

Puede controlar el Selectedevento , Updated, Insertedo Deleted para examinar y manipular las propiedades de una DbCommand vez enviado por el SqlDataSource control a la base de datos. La Command propiedad permite tener acceso a valores devueltos y valores en cualquier parámetro de salida después de que la operación de base de datos se realice a través de su Parameters propiedad, así como la CommandText propiedad , que representa la consulta SQL, el comando o el nombre del procedimiento almacenado que se envió a la base de datos.

Los parámetros de salida proceden específicamente de parámetros que tienen un InputOutput valor o Output para la Direction propiedad del Parameter objeto . Un valor devuelto procede de un parámetro que tiene un ReturnValue valor .

Se aplica a

Consulte también