下面的代码示例演示如何将 DataObjectMethodAttribute 属性应用于一个公共公开的方法,并标识该方法所执行的数据操作类型及该方法是否是该类型的默认数据方法。在此示例中,NorthwindData 类型公开两个数据方法:一个方法名为 GetAllEmployees,用于检索一组数据;另一个方法名为 DeleteEmployeeByID,用于删除数据。将 DataObjectMethodAttribute 属性同时应用于这两个方法,将 GetAllEmployees 方法标记为“Select”数据操作的默认方法,将 DeleteEmployeeByID 方法标记为“Delete”数据操作的默认方法。
C#
复制代码
[DataObjectAttribute]
public class NorthwindData
{
public NorthwindData() {}
[DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
public static IEnumerable GetAllEmployees()
{
AccessDataSource ads = new AccessDataSource();
ads.DataSourceMode = SqlDataSourceMode.DataReader;
ads.DataFile = "~//App_Data//Northwind.mdb";
ads.SelectCommand = "SELECT EmployeeID,FirstName,LastName FROM Employees";
return ads.Select(DataSourceSelectArguments.Empty);
}
// Delete the Employee by ID.
[DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
public void DeleteEmployeeByID(int employeeID)
{
throw new Exception("The value passed to the delete method is "
+ employeeID.ToString());
}
}