代码段:实现 Updater

上次修改时间: 2010年4月19日

适用范围: SharePoint Server 2010

本文内容
.NET 连接程序集的示例
ASP.NET Web 服务的示例
WCF 服务的示例
其他代码示例

下面的代码示例演示如何在 .NET 连接程序集和 Web 服务中实现 Updater 方法实例。

.NET 连接程序集的示例

public void UpdateCustomer(Customer customer)
{
    Customer oCustomer = GetCustomerByID(customer.CustomerID);
    oCustomer.Industry = customer.Industry;
    oCustomer.MobilePhoneNumber = customer.MobilePhoneNumber;
    oCustomer.Name = customer.Name;
    oCustomer.ParentCustomerID = customer.ParentCustomerID;
    oCustomer.WebSite = customer.WebSite;
    oCustomer.WorkPhoneNumber = customer.WorkPhoneNumber;
    oCustomer.Version++;
    oCustomer.ModifiedDate = DateTime.Now;
}

ASP.NET Web 服务的示例

[WebMethod]
public void UpdateCustomer(Customer customer)
{
    Customer oCustomer = GetCustomerByID(customer.CustomerID);
    oCustomer.Industry = customer.Industry;
    oCustomer.MobilePhoneNumber = customer.MobilePhoneNumber;
    oCustomer.Name = customer.Name;
    oCustomer.ParentCustomerID = customer.ParentCustomerID;
    oCustomer.WebSite = customer.WebSite;
    oCustomer.WorkPhoneNumber = customer.WorkPhoneNumber;
    oCustomer.Version++;
    oCustomer.ModifiedDate = DateTime.Now;
}

WCF 服务的示例

以下代码演示服务合同接口中的操作定义。

[OperationContract]
void UpdateCustomer(Customer customer);

以下示例演示方法实例的实现。

public void UpdateCustomer(Customer customer)
{
    Customer oCustomer = GetCustomerByID(customer.CustomerID);
    oCustomer.Industry = customer.Industry;
    oCustomer.MobilePhoneNumber = customer.MobilePhoneNumber;
    oCustomer.Name = customer.Name;
    oCustomer.ParentCustomerID = customer.ParentCustomerID;
    oCustomer.WebSite = customer.WebSite;
    oCustomer.WorkPhoneNumber = customer.WorkPhoneNumber;
    oCustomer.Version++;
    oCustomer.ModifiedDate = DateTime.Now;
}

其他代码示例

外部系统 - .NET 连接程序集

例如,对于 Microsoft SQL Server 数据库中的联系人实体,Updater 方法可能与以下代码类似。

public static void Update(Contact contact)
{
    const string ServerName = "MySQLServerName";
    AdventureWorksDataContext dataContext = new AdventureWorksDataContext
          ("Data Source=" + ServerName + ";" +
           "Initial Catalog=AdventureWorks;Integrated Security=True");

    var contactToUpdate = (from contacts in dataContext.Contacts
                            where contacts.ContactID == contact.ContactID
                            select contacts).Single();

    contactToUpdate.FirstName = contact.FirstName;
    contactToUpdate.LastName = contact.LastName;
    contactToUpdate.EmailAddress = contact.EmailAddress;
    contactToUpdate.Phone = contact.Phone;
    contactToUpdate.EmailPromotion = contact.EmailPromotion;
    contactToUpdate.NameStyle = contact.NameStyle;
    contactToUpdate.PasswordHash = contact.PasswordHash;
    contactToUpdate.PasswordSalt = contact.PasswordSalt;
    contactToUpdate.ModifiedDate = DateTime.Now;
    contactToUpdate.rowguid = Guid.NewGuid();
    dataContext.SubmitChanges();
}

请参阅

概念

实现 Updater