Фрагмент кода: реализация метода Updater
Дата последнего изменения: 19 апреля 2010 г.
Применимо к: SharePoint Server 2010
В этой статье
Пример для сборки подключения .NET
Пример для веб-службы ASP.NET
Пример для службы WCF
Дополнительные примеры кода
В следующем примере кода показана реализация экземпляра метода Updater в сборке подключения .NET и в веб-службе.
Пример для сборки подключения .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
[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
Например, для сущности Contact в базе данных Microsoft SQL Server метод обновления может выглядеть следующим образом.
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();
}