다음을 통해 공유


Code Snippet: Implementing an Updater

Applies to: SharePoint Server 2010

In this article
Example for a .NET Connectivity Assembly
Example for an ASP.NET Web Service
Example for a WCF Service
Additional Code Examples

The following code examples show how you can implement an Updater method instance in a .NET connectivity assembly and in a Web service.

Example for a .NET Connectivity Assembly

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;
}

Example for an ASP.NET Web Service

[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;
}

Example for a WCF Service

The following code shows the operation definition in the service contract interface.

[OperationContract]
void UpdateCustomer(Customer customer);

The following example shows the implementation of the method instance.

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;
}

Additional Code Examples

External System—.NET Connectivity Assembly

For example, for the Contact entity in a Microsoft SQL Server database, the Updater method might look similar to the following.

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();
}

See Also

Concepts

Implementing an Updater