SqlDataSourceStatusEventArgs 类

定义

SqlDataSource 控件在数据操作完成后引发的事件提供数据。Provides data for an event that is raised by the SqlDataSource control after a data operation has completed.

public ref class SqlDataSourceStatusEventArgs : EventArgs
public class SqlDataSourceStatusEventArgs : EventArgs
type SqlDataSourceStatusEventArgs = class
    inherit EventArgs
Public Class SqlDataSourceStatusEventArgs
Inherits EventArgs
继承
SqlDataSourceStatusEventArgs

示例

下面的代码示例演示如何使用SqlDataSourceStatusEventArgs类检查在SqlDataSource使用带有GridView存储过程的控件填充控件时返回的输出参数的返回值和值。The following code example demonstrates how to use the SqlDataSourceStatusEventArgs class to examine the return value and values of output parameters that are returned when using a SqlDataSource control with a stored procedure to populate a GridView control. 该存储过程选择中显示的数据, 但GridView也将其他信息传递回调用方, 如整数输出参数和返回值。The stored procedure selects data that is displayed in the GridView, but also passes other information back to the caller, such as an integer output parameter and a return value. 用于存储过程的SqlDataSource参数包含SelectParameters在集合中, 它包含将信息从 Web 窗体传递到存储过程的参数, 以及将信息传递回窗体.The parameters that the SqlDataSource uses for the stored procedure are contained by the SelectParameters collection, and consist of parameters that pass information from the Web form to the stored procedure as well as parameters that pass information back to the form. 这些Direction参数的属性设置为OutputReturnValueThe Direction property of these parameters is set to Output and ReturnValue.

<%@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>

注解

SqlDataSourceStatusEventArgs类在Updated、、和Inserted事件中用于在数据源控件执行后传递有关数据库操作的信息。 Deleted SelectedThe SqlDataSourceStatusEventArgs class is used in the Selected, Updated, Inserted, and Deleted events to pass information about a database operation after it is performed by the data source control. 此信息包括操作影响的行数、用于执行该操作DbCommand的数据源的对象, 以及产生的任何异常信息。This information includes the number of rows affected by the operation, the DbCommand object that the data source used to perform the operation, and any exception information that resulted. 通过添加事件处理程序委托Selected来处理InsertedUpdatedDeleted事件, 您可以检查此数据并执行所需的任何其他的后期处理。By adding an event handler delegate to handle the Selected, Updated, Inserted or Deleted events, you can examine this data and perform any additional post processing required.

SqlDataSource控件公开了许多事件, 您可以处理这些事件, 以便在数据操作过程中使用基础数据对象。The SqlDataSource control exposes many events that you can handle to work with the underlying data objects during the course of a data operation. 下表列出了事件和关联EventArgs的事件处理程序类, 以更好地指导您SqlDataSource使用控件的数据操作的生命周期。The following table lists the events and associated EventArgs and event handler classes, to better guide you to the various events that correspond to the life cycle of a data operation using the SqlDataSource control.

事件Event EventArgsEventArgs EventHandlerEventHandler
Selecting在检索数据之前发生。Selecting occurs before the data is retrieved. SqlDataSourceSelectingEventArgs SqlDataSourceSelectingEventHandler
Inserting在执行插入、更新或删除操作之前发生。Deleting UpdatingInserting, Updating, Deleting occur before an insert, update, or delete operation is performed. SqlDataSourceCommandEventArgs SqlDataSourceCommandEventHandler
SelectedInserted数据检索Deleted 、插入、更新或删除操作完成后, 将发生、、 UpdatedSelected, Inserted, Updated, Deleted occur after the data retrieval, insert, update, or delete operations completes. SqlDataSourceStatusEventArgs SqlDataSourceStatusEventHandler

构造函数

SqlDataSourceStatusEventArgs(DbCommand, Int32, Exception)

使用指定的输出参数、返回值和此数据库操作影响的行数初始化 SqlDataSourceStatusEventArgs 类的新实例。Initializes a new instance of the SqlDataSourceStatusEventArgs class, using the specified output parameters, return value, and number of rows affected by the database operation.

属性

AffectedRows

获取受数据库操作影响的行数。Gets the number of rows affected by a database operation.

Command

获取提交到数据库的数据库命令。Gets the database command submitted to the database.

Exception

为数据库在数据操作期间引发的任何异常获取包装。Gets a wrapper for any exceptions thrown by the database during a data operation.

ExceptionHandled

获取或设置一个值,该值指示是否已处理数据库引发的异常。Gets or sets a value indicating whether an exception thrown by the database has been handled.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅