代码段:实现 SpecificFinder
上次修改时间: 2010年4月19日
适用范围: SharePoint Server 2010
本文内容
.NET 连接程序集的示例
ASP.NET Web 服务的示例
WCF 服务的示例
其他代码示例
以下代码示例演示如何在 .NET 连接程序集和 Web 服务中实现 SpecificFinder 方法实例。
.NET 连接程序集的示例
public Customer GetCustomerByID(String id)
{
foreach (Customer customer in customers)
{
if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
{
return customer;
}
}
throw new ArgumentOutOfRangeException("id", "Customer not found");
}
ASP.NET Web 服务的示例
[WebMethod]
public Customer GetCustomerByID(String id)
{
foreach (Customer customer in customers)
{
if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
{
return customer;
}
}
throw new ArgumentOutOfRangeException("id", "Customer not found");
}
WCF 服务的示例
下面的代码演示服务约定接口中的操作定义。
[OperationContract]
Customer GetCustomerByID(string id);
下面的示例演示方法实例的实现。
public Customer GetCustomerByID(String id)
{
foreach (Customer customer in customers)
{
if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
{
return customer;
}
}
throw new ArgumentOutOfRangeException("id", "Customer not found");
}
其他代码示例
外部系统 - 数据库/SQL Server
例如,对于 Microsoft SQL Server 数据库中的 Contact 实体,SpecificFinder 方法的外观可能与以下内容类似。
public static Contact ReadItem(int contactID) { const string ServerName = "MySQLServerName"; AdventureWorksDataContext dataContext = new AdventureWorksDataContext ("Data Source=" + ServerName + ";" + "Initial Catalog=AdventureWorks;Integrated Security=True"); Contact Contact = (from contacts in dataContext.Contacts.AsEnumerable() where contacts.ContactID == contactID select contacts).Single(); return Contact; }
外部系统 - 平面文件
public static IEnumerable<FlatFileEntity> ReadList() { List<FlatFileEntity> flatFileEntityList = new List<FlatFileEntity>(); TextReader textReader = new StreamReader(@"c:\data\flat-file-data-source.txt"); string row; while ((row = textReader.ReadLine()) != null) { FlatFileEntity flatFileEntity = new FlatFileEntity(); string[] entityData = row.Split(','); flatFileEntity.ID = entityData[0]; flatFileEntity.Company = entityData[1]; flatFileEntity.FirstName = entityData[2]; flatFileEntity.LastName = entityData[3]; flatFileEntity.Address = entityData[4]; flatFileEntity.City = entityData[5]; flatFileEntity.State = entityData[6]; flatFileEntity.ZipCode = entityData[7]; flatFileEntity.Phone = entityData[8]; flatFileEntity.LastUpdated = DateTime.Parse(entityData[9]); flatFileEntityList.Add(flatFileEntity); } textReader.Close(); foreach (FlatFileEntity entity in flatFileEntityList) { if (entity.ID == id) return entity; } return null; }