日期:2014-05-20 浏览次数:20910 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Data.Linq.Mapping; namespace Modifying_Data_Using_Linq_To_SQL { //主程序 public class Tester { static void Main() { AddCustomer(); UpdateCustomer(); Console.ReadKey(); } private static void AddCustomer() { Console.WriteLine("Adding a new customer..."); MyNewItemDataContext dc = new MyNewItemDataContext(); //加入带有地址的新客户 Customer douglas = new Customer(); douglas.FirstName = "Douglas"; douglas.LastName = "Adams"; douglas.EmailAddress = "douglas0@adventureworks.com"; douglas.PasswordHash = "fake"; douglas.PasswordSalt = "fake"; douglas.ModifiedDate = DateTime.Today; douglas.rowguid = Guid.NewGuid(); Address addr = new Address(); addr.AddressLine1 = "1c sharp Way"; addr.City = "Seattle"; addr.PostalCode = "98011"; addr.StateProvince = "Washington"; addr.CountryRegion = "United States"; addr.ModifiedDate = DateTime.Today; addr.rowguid = Guid.NewGuid(); CustomerAddress ca = new CustomerAddress(); ca.AddressType = "Main Office"; ca.Address = addr; ca.Customer = douglas; ca.ModifiedDate = DateTime.Today; ca.rowguid = Guid.NewGuid(); dc.Customers.Add(douglas); dc.SubmitChanges(); ShowCustomersByFirstName("Douglas"); } //更新客户记录 private static void UpdateCustomer() { Console.WriteLine("Updating a customer..."); MyNewItemDataContext dc = new MyNewItemDataContext(); Customer dAdams = dc.Customers.Single( c => (c.FirstName == "Douglas" && c.LastName == "Adams")); Console.WriteLine("Before:\n{0}", dAdams); dAdams.Title = "Mr."; //加一个新的发货地址 Address addr = new Address(); addr.AddressLine1 = "1 Warehouse Place"; addr.City = "Los Angeles"; addr.PostalCode = "30210"; addr.StateProvince = "California"; addr.CountryRegion = "United States"; addr.ModifiedDate = DateTime.Today; addr.rowguid = Guid.NewGuid(); CustomerAddress ca = new CustomerAddress(); ca.AddressType = "Shipping"; ca.Address = addr; ca.Customer = dAdams; ca.ModifiedDate = DateTime.Today; ca.rowguid = Guid.NewGuid(); dc.SubmitChanges(); Customer dAdams1 = dc.Customers.Single( c => (c.FirstName == "Douglas" && c.LastName == "Adams")); Console.WriteLine("After:\n{0}", dAdams); } //查找带有制定名字的客户记录列表 private static void ShowCustomersByFirstName(string firstname) { MyNewItemDataContext dc = new MyNewItemDataContext(); var customers = from customer in dc.Customers where customer.FirstName == "Douglas" orderby customer.FirstName, customer.LastName select customer; Console.WriteLine("Customers whose first name is {0}:", firstname); foreach (Customer customer in customers) Console.WriteLine(customer); } } //对创建的客户类加入一个方法来格式化显示客户属性 public partial class Customer { public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0} {1} {2} {3}", Title, FirstName, LastName, EmailAddress); foreach (CustomerAddress ca in CustomerAddresses) { sb.AppendFormat("\n\t{0}: {1}, {2}", ca.AddressType, ca.Address.AddressLine1, ca.Address.City); } sb.AppendLine(); return sb.ToString(); } } }