次の方法で共有


コード スニペット: Updater の実装

最終更新日: 2010年4月19日

適用対象: SharePoint Server 2010

この記事の内容
.NET Connectivity Assembly での例
ASP.NET Web サービスでの例
WCF サービスでの例
その他のコード例

以下のコード例は, .NET Connectivity Assembly と Web サービスで Updater メソッド インスタンスを実装する方法を示します。

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

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 Connectivity Assembly

たとえば、Microsoft SQL Server データベースの Contact エンティティに対しては、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 の実装