云中剑阁

虽然我现在还是一棵小小的树苗,但是不久的将来,我一定会成长成一棵参天大树
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ObjectDataSource类详解 -MSDN

Posted on 2008-01-28 18:44  小恒  阅读(3551)  评论(0编辑  收藏  举报
.NET Framework 类库
ObjectDataSource 类

注意:此类在 .NET Framework 2.0 版中是新增的。

表示合并参数后解析方法多层 Web 应用程序结构中的数据绑定控件提供数据的业务对象。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

Expand 图像语法

Collapse 图像备注

ObjectDataSource 是 ASP.NET 数据源控件,用于向数据绑定控件表示识别数据的中间层对象或数据接口对象。可以结合使用 ObjectDataSource 控件与数据绑定控件,这样,只用少量代码或不用代码就可以在网页上显示、编辑和排序数据。

非常常见的应用程序设计做法是将表示层同业务逻辑分开,并在业务对象中封装业务逻辑。这些业务对象在表示层和数据层之间构成一个独特的层,从而得到一个三层应用程序结构。ObjectDataSource 控件使开发人员能够在保留他们的三层应用程序结构的同时,使用 ASP.NET 数据源控件。

ObjectDataSource 控件使用反射创建业务对象的实例,并调用这些实例的方法以检索、更新、插入和删除数据。TypeName 属性标识 ObjectDataSource 使用的类的名称。ObjectDataSource 控件在每次调用方法时都创建并销毁类的实例,它在 Web 请求的生存期内不在内存中保留对象。如果您使用的业务对象需要很多资源或者在其他方面需要很大开销来创建和销毁,您就需要认真考虑。使用高开销对象可能并不是最佳的设计选择,但是可以使用 ObjectCreatingObjectCreatedObjectDisposing 事件来控制该对象的生存期。

Note注意

SelectMethodUpdateMethodInsertMethodDeleteMethod 属性标识的方法可以是实例方法或 static(在 Visual Basic 中为 Shared)方法。如果方法为 static(在 Visual Basic 中为 Shared),则不创建业务对象的实例,也不引发 ObjectCreating、ObjectCreated 和 ObjectDisposing 事件。

若要从业务对象中检索数据,请用检索数据的方法的名称设置 SelectMethod 属性。如果此方法未返回 IEnumerableDataSet 对象,则运行时在 IEnumerable 集合中包装该对象。如果方法签名带参数,您可以将 Parameter 对象添加到 SelectParameters 集合,然后将它们绑定到要传递给由 SelectMethod 方法指定的方法的值。为使 ObjectDataSource 能够使用参数,这些参数必须与方法签名中的参数名称和类型相匹配。

每次调用 Select 方法时,ObjectDataSource 控件都检索数据。此方法提供对 SelectMethod 属性所指定的方法的编程访问。当调用绑定到 ObjectDataSource 的控件的 DataBind 方法时,这些控件自动调用 SelectMethod 属性指定的方法。如果设置数据绑定控件的 DataSourceID 属性,该控件根据需要自动绑定到数据源中的数据。建议通过设置 DataSourceID 属性将 ObjectDataSource 控件绑定到数据绑定控件。或者,可以设置 DataSource 属性,但之后必须显式调用数据绑定控件的 DataBind 方法。可以随时以编程方式调用 Select 方法以检索数据。

有关将数据绑定控件绑定到数据源控件的更多信息,请参见使用数据源控件绑定到数据

根据 ObjectDataSource 控件使用的业务对象的功能,可以执行数据操作,如更新、插入和删除。若要执行这些数据操作,请为要执行的操作设置适当的方法名称和任何关联的参数。例如,对于更新操作,将 UpdateMethod 属性设置为业务对象方法的名称,该方法执行更新并将所需的任何参数添加到 UpdateParameters 集合中。如果 ObjectDataSource 控件与数据绑定控件相关联,则由数据绑定控件添加参数。这种情况下,需要确保方法的参数名称与数据绑定控件中的字段名称相匹配。调用 Update 方法时,由代码显式执行更新或由数据绑定控件自动执行更新。DeleteInsert 操作遵循相同的常规模式。假定业务对象以逐个记录(而不是以批处理)的方式执行这些类型的数据操作。

如果数据作为 DataSet、DataViewDataTable 对象返回,ObjectDataSource 控件可以筛选由 SelectMethod 属性检索的数据。ObjectDataSource 控件允许缓存所有类型的数据,但您不应缓存那些保留不能被共享以服务于多个请求的资源或状态的对象(例如,打开的 SqlDataReader 对象),因为对象的此实例将用于服务多个请求。您可以使用格式字符串语法将 FilterExpression 属性设置为筛选表达式,并将表达式中的值绑定到 FilterParameters 集合中指定的参数。

尽管 ObjectDataSource 不在多个请求之间保留业务对象的实例,但它可以缓存 SelectMethod 方法的结果。缓存数据时,对 Select 方法的后续调用将返回缓存的数据,而不是创建业务对象并使用反射来调用该对象的 SelectMethod。缓存可避免以 Web 服务器内存开销的代价创建对象和调用对象的数据方法;这可能是一个好的折中方法。当 EnableCaching 属性设置为 true 且 CacheDuration 属性设置为在丢弃缓存前缓存存储数据的秒数时,ObjectDataSource 将自动缓存数据。也可以指定 CacheExpirationPolicy 属性和可选的 SqlCacheDependency 属性。

下表描述 ObjectDataSource 控件的功能。

功能

要求

选择

将 SelectMethod 属性设置为业务对象方法的名称,该方法选择数据并以编程方式或使用数据绑定控件,在 SelectParameters 集合中包含所有必需的参数。

排序

SortParameterName 属性设置为带有排序条件的 SelectMethod 方法中的参数名称。

筛选

将 FilterExpression 属性设置为一个筛选表达式,并选择将任何参数添加到 FilterParameters 集合中,以在调用 Select 方法时筛选数据。SelectMethod 属性指定的方法必须返回 DataSet、DataView 或 DataTable。

分页

如果 SelectMethod 方法包含要检索的最大记录数的参数和要检索的第一个记录的索引参数,则支持数据源分页。必须分别在 MaximumRowsParameterNameStartRowIndexParameterName 属性中设置那些参数的名称。即使 ObjectDataSource 控件不支持直接在 SelectMethod 属性指定的方法中分页,数据绑定控件自己也可能能够执行分页。要使数据绑定控件能够执行分页,SelectMethod 属性指定的方法必须返回一个实现 Icollection 接口的对象。

更新

将 UpdateMethod 属性设置为业务对象方法的名称,该方法更新数据并在 UpdateParameters 集合中包含所有必需的参数。

删除

将 DeleteMethod 属性设置为业务对象方法或函数的名称,该方法或函数删除数据并在 DeleteParameters 集合中包含所有必需的参数。

插入

将 InsertMethod 属性设置为业务对象方法或函数的名称,该方法或函数插入数据并在 InsertParameters 集合中包含所有必需的参数。

缓存

根据缓存数据的缓存行为,将 EnableCaching 属性设置为 true,并设置 CacheDuration 和 CacheExpirationPolicy 属性。

与所有数据源控件一样,ObjectDataSource 控件与数据源视图类关联。ObjectDataSource 控件是页面开发人员用来使用数据的接口,而 ObjectDataSourceView 类是数据绑定控件使用的接口。此外,ObjectDataSourceView 类描述数据源控件的功能并执行实际的工作。ObjectDataSource 控件仅有一个关联的 ObjectDataSourceView,而且它始终命名为 DefaultView。虽然 ObjectDataSourceView 对象由 GetView 方法公开,但它的许多属性和方法都是直接由 ObjectDataSource 控件包装和公开的。在后台,ObjectDataSourceView 对象执行所有数据操作,包括检索、插入、更新、删除、筛选和排序数据。有关更多信息,请参见 ObjectDataSourceView。

ObjectDataSource 控件没有可视呈现,它是作为控件实现的,以便您可以以声明方式创建它,还可以选择让它参与状态管理。因此,ObjectDataSource 不支持可视功能,如 EnableThemingSkinID 属性。

主题 位置
如何:使用数据源控件时保证连接字符串的安全性 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 Repeater Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:确保使用数据源控件时连接字符串的安全 生成 ASP .NET Web 应用程序
如何:绑定到 Visual Studio 的模板化控件中的数据 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:绑定到模板化控件中的数据 生成 ASP .NET Web 应用程序
演练:具有 Visual Studio 数据组件的数据绑定网页 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中构建 ASP .NET Web 应用程序

Collapse 图像示例

本节包含两个代码示例。第一个代码示例演示 GridView 控件如何使用 ObjectDataSource 对象在 Web 窗体页上显示数据。第二个代码示例提供由此代码示例和许多其他 ObjectDataSource 代码示例使用的中间层业务对象的示例。

下面的代码示例演示 GridView 控件如何使用 ObjectDataSource 控件在 Web 窗体页上显示数据。ObjectDataSource 用它的 TypeName 属性标识业务对象的部分或完全限定类名,用它的 SelectMethod 属性标识为检索数据而调用的业务对象方法。在运行时创建该对象,并使用反射功能调用该方法。GridView 控件枚举通过 SelectMethod 返回的 IEnumerable 集合并显示数据。

Visual Basic  CopyCode image复制代码
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<html>
  <head>
    <title>ObjectDataSource - Visual Basic Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.VB.EmployeeLogic" />

    </form>
  </body>
</html>
C#  CopyCode image复制代码
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<html>
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.CS.EmployeeLogic" />

    </form>
  </body>
</html>
J#  CopyCode image复制代码
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %>
<%@ Page language="VJ#" %>
<html>
  <head>
    <title>ObjectDataSource - VJ# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1" />

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.JSL.EmployeeLogic" />

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

下面的代码示例提供由此示例和许多其他 ObjectDataSource 代码示例使用的中间层业务对象的示例。此代码示例由以下两个基类组成:

  • EmployeeLogic 类,这是封装业务逻辑的无状态类。

  • NorthwindEmployee 类,这是仅包含从数据层加载和保留数据所需的基本功能的模型类。

提供附加 NorthwindDataException 类只是为了便于使用。

这组示例类与 Northwind Traders 数据库一起使用,该数据库是 Microsoft SQL Server 和 Microsoft Access 自带的数据库。要获得完整的可运行示例,您必须结合所提供的 Web 窗体代码示例来编译和使用这些类。

此示例的设计简单、易用,它演示了业务对象与 ObjectDataSource 控件之间最常见的交互方式之一。它不是设计建议。在一些示例中,将其他方法添加到了 EmployeeLogicNorthwindEmployee 类,或对这组类进行了修改以演示基本概念。

Visual Basic  CopyCode image复制代码
Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB
'
' EmployeeLogic is a stateless business object that encapsulates
' the operations you can perform on a NorthwindEmployee object.
' When the class is written in Visual Basic, you cannot use the Shared
' part.
Public Class EmployeeLogic
   ' Returns a collection of NorthwindEmployee objects.
   Public Shared Function GetAllEmployees() As ICollection
      Dim al As New ArrayList()

      Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
      Dim sds As New SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees")
      Try
         Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty)

         ' Iterate through the Enumeration and create a
         ' NorthwindEmployee object for each ID.
         For Each row As DataRowView In IDs
            Dim id As String = row("EmployeeID").ToString()
            Dim nwe As New NorthwindEmployee(id)
            ' Add the NorthwindEmployee object to the collection.
            al.Add(nwe)
         Next
      Finally
         ' If anything strange happens, clean up.
         sds.Dispose()
      End Try

      Return al
   End Function 'GetAllEmployees

   Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee
      Return New NorthwindEmployee(anID)
   End Function 'GetEmployee


   Public Shared Sub UpdateEmployeeInfo(ne As NorthwindEmployee)
      Dim retval As Boolean = ne.Save()
      If Not retval Then
         Throw New NorthwindDataException("UpdateEmployee failed.")
      End If
   End Sub 'UpdateEmployeeInfo

   Public Shared Sub DeleteEmployee(ne As NorthwindEmployee)
   End Sub 'DeleteEmployee

End Class 'EmployeeLogic


Public Class NorthwindEmployee


   Public Sub New()
      ID = DBNull.Value
      aLastName = ""
      aFirstName = ""
      aTitle = ""
      titleOfCourtesy = ""
      reportsTo = - 1
   End Sub 'New


   Public Sub New(anID As Object)
      Me.ID = anID
      Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection")
      Dim conn As New SqlConnection(cts.ConnectionString)
      Dim sc As New SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " & _
                               " FROM Employees " & _
                               " WHERE EmployeeID = @empId", conn)
      ' Add the employee ID parameter and set its value.
      sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString())
      Dim sdr As SqlDataReader = Nothing

      Try
         conn.Open()
         sdr = sc.ExecuteReader()

         ' This is not a while loop. It only loops once.
         If Not (sdr Is Nothing) AndAlso sdr.Read() Then
            ' The IEnumerable contains DataRowView objects.
            Me.aFirstName = sdr("FirstName").ToString()
            Me.aLastName = sdr("LastName").ToString()
            Me.aTitle = sdr("Title").ToString()
            Me.titleOfCourtesy = sdr("TitleOfCourtesy").ToString()
            If Not sdr.IsDBNull(4) Then
               Me.reportsTo = sdr.GetInt32(4)
            End If
         Else
            Throw New NorthwindDataException("Data not loaded for employee id.")
         End If
      Finally
         Try
            If Not (sdr Is Nothing) Then
               sdr.Close()
            End If
            conn.Close()
         Catch se As SqlException
            ' Log an event in the Application Event Log.
            Throw
         End Try
      End Try
   End Sub 'New

   Private ID As Object

   Private aLastName As String
   Public Property LastName() As String
      Get
         Return aLastName
      End Get
      Set
         aLastName = value
      End Set
   End Property

   Private aFirstName As String
   Public Property FirstName() As String
      Get
         Return aFirstName
      End Get
      Set
         aFirstName = value
      End Set
   End Property

   Private aTitle As String
   Public Property Title() As String
      Get
         Return aTitle
      End Get
      Set
         aTitle = value
      End Set
   End Property

   Private titleOfCourtesy As String
   Public Property Courtesy() As String
      Get
         Return titleOfCourtesy
      End Get
      Set
         titleOfCourtesy = value
      End Set
   End Property
   Private reportsTo As Integer

   Public Property Supervisor() As Integer
      Get
         Return reportsTo
      End Get
      Set
         reportsTo = value
      End Set
   End Property

   Public Function Save() As Boolean
      Return True
   End Function 'Save
End Class 'NorthwindEmployee


Friend Class NorthwindDataException
   Inherits Exception

   Public Sub New(msg As String)
      MyBase.New(msg)
   End Sub 'New
End Class 'NorthwindDataException
End Namespace
C#  CopyCode image复制代码
namespace Samples.AspNet.CS {

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
  //
  // EmployeeLogic is a stateless business object that encapsulates
  // the operations one can perform on a NorthwindEmployee object.
  //
  public class EmployeeLogic {

    // Returns a collection of NorthwindEmployee objects.
    public static ICollection GetAllEmployees () {
      ArrayList al = new ArrayList();

      ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];

      SqlDataSource sds
        = new SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees");

      try {

        IEnumerable IDs = sds.Select(DataSourceSelectArguments.Empty);

        // Iterate through the Enumeration and create a
        // NorthwindEmployee object for each ID.
        foreach (DataRowView row in IDs) {
          string id = row["EmployeeID"].ToString();
          NorthwindEmployee nwe = new NorthwindEmployee(id);
          // Add the NorthwindEmployee object to the collection.
          al.Add(nwe);
        }
      }
      finally {
        // If anything strange happens, clean up.
        sds.Dispose();
      }

      return al;
    }
    public static NorthwindEmployee GetEmployee(object anID) {
      return new NorthwindEmployee(anID);
    }

    public static void UpdateEmployeeInfo(NorthwindEmployee ne) {
      bool retval = ne.Save();
      if (! retval) { throw new NorthwindDataException("UpdateEmployee failed."); }
    }

    public static void DeleteEmployee(NorthwindEmployee ne) { }

  }

  public class NorthwindEmployee {

    public NorthwindEmployee () {
      ID = DBNull.Value;
      lastName = "";
      firstName = "";
      title="";
      titleOfCourtesy = "";
      reportsTo = -1;
    }

    public NorthwindEmployee (object anID) {
      this.ID = anID;

      ConnectionStringSettings cts = ConfigurationManager.ConnectionStrings["NorthwindConnection"];

		SqlConnection conn = new SqlConnection (cts.ConnectionString);
      SqlCommand sc =
        new SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " +
                       " FROM Employees " +
                       " WHERE EmployeeID = @empId",
                       conn);
      // Add the employee ID parameter and set its value.
      sc.Parameters.Add(new SqlParameter("@empId",SqlDbType.Int)).Value = Int32.Parse(anID.ToString());
      SqlDataReader sdr = null;

      try {
        conn.Open();
        sdr = sc.ExecuteReader();

        // This is not a while loop. It only loops once.
        if (sdr != null && sdr.Read()) {
          // The IEnumerable contains DataRowView objects.
          this.firstName        = sdr["FirstName"].ToString();
          this.lastName         = sdr["LastName"].ToString();
          this.title            = sdr["Title"].ToString();
          this.titleOfCourtesy  = sdr["TitleOfCourtesy"].ToString();
          if (! sdr.IsDBNull(4)) {
            this.reportsTo        = sdr.GetInt32(4);
          }
        }
        else {
          throw new NorthwindDataException("Data not loaded for employee id.");
        }
      }
      finally {
        try {
          if (sdr != null) sdr.Close();
          conn.Close();
        }
        catch (SqlException) {
          // Log an event in the Application Event Log.
          throw;
        }
      }
    }

    private object ID;

    private string lastName;
    public string LastName {
      get { return lastName; }
      set { lastName = value; }
    }

    private string firstName;
    public string FirstName {
      get { return firstName; }
      set { firstName = value;  }
    }

    private string title;
    public String Title {
      get { return title; }
      set { title = value; }
    }

    private string titleOfCourtesy;
    public string Courtesy {
      get { return titleOfCourtesy; }
      set { titleOfCourtesy = value; }
    }

    private int    reportsTo;
    public int Supervisor {
      get { return reportsTo; }
      set { reportsTo = value; }
    }

    public bool Save () {
      return true;
    }
  }

  internal class NorthwindDataException: Exception {
    public NorthwindDataException(string msg) : base (msg) { }
  }
}
J#  CopyCode image复制代码
package  Samples.AspNet.JSL; 

import System.*;
import System.Collections.*;
import System.Collections.Specialized.*;
import System.Configuration.*;
import System.Data.*;
import System.Data.SqlClient.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
   
//
// EmployeeLogic is a stateless business object that encapsulates
// the operations one can perform on a NorthwindEmployee object.
//
public class EmployeeLogic
{
    // Returns a collection of NorthwindEmployee objects.
    public static ICollection GetAllEmployees()
        throws NorthwindDataException, SqlException
    {
        ArrayList al = new ArrayList();

        ConnectionStringSettings cts = 
            ConfigurationManager.get_ConnectionStrings().get_Item("NorthwindConnection");

		
        SqlDataSource sds =
            new SqlDataSource(cts.get_ConnectionString(), "SELECT EmployeeID FROM Employees");

        try {
            IEnumerable ids = sds.Select(DataSourceSelectArguments.get_Empty());

            // Iterate through the Enumeration and create a
            // NorthwindEmployee object for each id.
            IEnumerator enumerator = ids.GetEnumerator();
            while (enumerator.MoveNext()) {
                // The IEnumerable contains DataRowView objects.
                DataRowView row = (DataRowView)enumerator.get_Current();
                String id = row.get_Item("EmployeeID").ToString();
                NorthwindEmployee nwe = new NorthwindEmployee(id);
                // Add the NorthwindEmployee object to the collection.
                al.Add(nwe);
            }
        }
        finally {
            // If anything strange happens, clean up.
            sds.Dispose();
        }

        return al;
    } //GetAllEmployees

    public static NorthwindEmployee GetEmployee(Object anId)
        throws NorthwindDataException, SqlException
    {
        return new NorthwindEmployee(anId);
    } //GetEmployee

    public static void UpdateEmployeeInfo(NorthwindEmployee ne)
        throws NorthwindDataException, SqlException
    {
        boolean retval = ne.Save();
        if (!retval) {
            throw new NorthwindDataException("UpdateEmployee failed.");
        }
    } //UpdateEmployeeInfo

    public static void DeleteEmployee(NorthwindEmployee ne)
    {
    } //DeleteEmployee 
} //EmployeeLogic

public class NorthwindEmployee
{
    public NorthwindEmployee()
    {
        id = DBNull.Value;
        lastName = "";
        firstName = "";
        title = "";
        titleOfCourtesy = "";
        reportsTo = -1;
    } //NorthwindEmployee

    public NorthwindEmployee(Object anId)
        throws NorthwindDataException, SqlException
    {
        this.id = anId;

        ConnectionStringSettings cts = 
	    ConfigurationManager.get_ConnectionStrings().get_Item("NorthwindConnection");
	SqlConnection conn = new SqlConnection(cts.get_ConnectionString());
        SqlCommand sc = new SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo" +
            " FROM Employees " +
            " WHERE EmployeeID = @empId",
            conn);
        // Add the employee ID parameter and set its value.
        sc.get_Parameters().Add(new SqlParameter("@empId", SqlDbType.Int)).
            set_Value((Int32)Int32.Parse(anId.ToString()));
        SqlDataReader sdr = null;

        try {
            conn.Open();
            sdr = sc.ExecuteReader();
            // This is not a while loop. It only loops once.
            if (sdr != null && sdr.Read()) {
                // The IEnumerable contains DataRowView objects.
                this.firstName = sdr.get_Item("FirstName").ToString();
                this.lastName = sdr.get_Item("LastName").ToString();
                this.title = sdr.get_Item("Title").ToString();
                this.titleOfCourtesy = sdr.get_Item("TitleOfCourtesy").
                    ToString();
                if (!(sdr.IsDBNull(4))) {
                    this.reportsTo = sdr.GetInt32(4);
                }
            }
            else {
                throw new NorthwindDataException("Data not loaded for employee id.");
            }
        }
        finally {
            try {
                if (sdr != null) {
                    sdr.Close();
                }
                conn.Close();
            }
            catch (SqlException exp) {
                // Log an event in the Application Event Log.
                throw exp;
            }
        }
    } //NorthwindEmployee

    private Object id;

    private String lastName;

    /** @property 
     */
    public String get_LastName()
    {
        return lastName;
    } //get_LastName

    /** @property 
     */
    public void set_LastName(String value)
    {
        lastName = value;
    } //set_LastName

    private String firstName;

    /** @property 
     */
    public String get_FirstName()
    {
        return firstName;
    } //get_FirstName

    /** @property 
     */
    public void set_FirstName(String value)
    {
        firstName = value;
    } //set_FirstName

    private String title;

    /** @property 
     */
    public String get_Title()
    {
        return title;
    } //get_Title

    /** @property 
     */
    public void set_Title(String value)
    {
        title = value;
    } //set_Title

    private String titleOfCourtesy;

    /** @property 
     */
    public String get_Courtesy()
    {
        return titleOfCourtesy;
    } //get_Courtesy

    /** @property 
     */
    public void set_Courtesy(String value)
    {
        titleOfCourtesy = value;
    } //set_Courtesy

    private int reportsTo;

    /** @property 
     */
    public int get_Supervisor()
    {
        return reportsTo;
    } //get_Supervisor

    /** @property 
     */
    public void set_Supervisor(int value)
    {
        reportsTo = value;
    } //set_Supervisor

    public boolean Save()
    {
        return true;
    } //Save
} //NorthwindEmployee

public class NorthwindDataException extends System.Exception
{
    public NorthwindDataException(String msg)
    {
         super(msg);
    } //NorthwindDataException
} //NorthwindDataException

Collapse 图像.NET Framework 安全性

  • AspNetHostingPermission  用于在宿主环境中进行操作。要求值:LinkDemand;权限值:Minimal
  • AspNetHostingPermission  用于在宿主环境中进行操作。要求值:InheritanceDemand;权限值:Minimal。

Collapse 图像继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.DataSourceControl
      System.Web.UI.WebControls.ObjectDataSource

Collapse 图像线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Collapse 图像平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

Collapse 图像版本信息

.NET Framework

受以下版本支持:2.0