Фрагмент кода: реализация автора
Дата последнего изменения: 19 апреля 2010 г.
Применимо к: SharePoint Server 2010
В этой статье
Пример для сборки подключения .NET
Пример для веб-службы ASP.NET
Пример для службы WCF
Дополнительные примеры кода
В следующем примере кода показана реализация экземпляра метода Creator в сборке подключения .NET и в веб-службе.
Пример для сборки подключения .NET
public String CreateCustomer(Customer customer)
{
customers.Add(customer);
return customer.CustomerID;
}
Пример для веб-службы ASP.NET
[WebMethod]
public String CreateCustomer(Customer customer)
{
customers.Add(customer);
return customer.CustomerID;
}
Пример для службы WCF
В следующем коде показано определение операции в интерфейсе контракта службы.
[OperationContract]
string CreateCustomer(Customer customer);
В следующем примере показана реализация экземпляра метода.
public String CreateCustomer(Customer customer)
{
customers.Add(customer);
return customer.CustomerID;
}
Дополнительные примеры кода
Внешняя система — сборка подключения .NET
Например, для сущности Contact в базе данных Microsoft SQL Server метод Creator может выглядеть примерно следующим образом.
public static Contact Create(Contact newContact)
{
const string ServerName = "MySQLServerName";
AdventureWorksDataContext dataContext = new AdventureWorksDataContext
("Data Source=" + ServerName + ";" +
"Initial Catalog=AdventureWorks;Integrated Security=True");
Contact contact = new Contact();
contact.FirstName = newContact.FirstName;
contact.LastName = newContact.LastName;
contact.EmailAddress = newContact.EmailAddress;
contact.Phone = newContact.Phone;
contact.EmailPromotion = newContact.EmailPromotion;
contact.NameStyle = newContact.NameStyle;
contact.PasswordHash = newContact.PasswordHash;
contact.PasswordSalt = newContact.PasswordSalt;
contact.ModifiedDate = DateTime.Now;
contact.rowguid = Guid.NewGuid();
dataContext.Contacts.InsertOnSubmit(contact);
dataContext.SubmitChanges();
return contact;
}