1:插入(Insert)

   (1)简单形式

      说明:new一个对象,使用Insert方法将其加入到对应的集合中。

Northwinddb = new Northwind("数据库连接字符串");
var newCustomer = new Customer
            {
                CustomerID = "XX1",
                CompanyName = "Company1",
                ContactName = "Contact1",
                City = "Seattle",
                Country = "USA"
            };
db.Customers.Insert(newCustomer);")

语句描述:使用Insert方法将新客户添加到Customers 表对象。

 注:生成的Sql语句为:

INSERT INTO Customers(CustomerID, ContactName, CompanyName, Phone, City, Country)
VALUES (@p0, @p1, @p2, @p3, @p4, @p5)
-- p0 = [XX1]
-- p1 = [Contact1]
-- p2 = [Company1]
-- p3 = NULL
-- p4 = [Seattle]
-- p5 = [USA]

 

 (2)一对多关系

说明:Orders和Customers是一对多的关系

 var newCustomer = new Customer
            {
                CustomerID = "XX4",
                CompanyName = "Company4",
                ContactName = "Contact4",
                City = "Seattle",
                Country = "USA"
            };
            var newOrder = new Order
            {  
                OrderDate = DateTime.Today,
                CustomerID=newCustomer.CustomerID
            };
            db.Customers.Insert(newCustomer);
            db.Orders.Insert(newOrder);

 

注:生成的Sql语句为:

INSERT INTO Customers(CustomerID, ContactName, CompanyName, Phone, City, Country)
VALUES (@p0, @p1, @p2, @p3, @p4, @p5)
-- p0 = [XX4]
-- p1 = [Contact4]
-- p2 = [Company4]
-- p3 = NULL
-- p4 = [Seattle]
-- p5 = [USA]

INSERT INTO Orders(CustomerID, OrderDate)
VALUES (@p0, @p1)
-- p0 = [XX4]
-- p1 = [11/07/2012 00:00:00]

(3):多对多关系

说明:在多对多关系中我们需要依次提交

2:更新(Update)

说明:更新操作,先获取对象,进行修改操作之后,直接调用Update()方法即可提交。

注:这里是在同一个数据库中

var customer= new Customer
            {
                CustomerID = "XX1",
                CompanyName = "Company1",
                ContactName = "Contact1",
                City = "Portland", 
                Country = "USA"
            };

db.Customers.Update(customer, d => d.City == "Detroit");
语句描述:使用Update将对检索到的一个Customer对象做出的更新保持回数据库。

3:删除(Delete)

说明:调用Delete方法即可

var customer = new Customer
            {
                CustomerID = "XX1",
                CompanyName = "Company1",
                ContactName = "Contact1",
                City = "Seattle",
                Country = "USA"
            };

  db.Customers.Delete(customer);

语句描述:使用Delete方法从Customer表中删除customer对象

 

Last edited Nov 7, 2012 at 8:35 AM by lf_dream8, version 25

Comments

No comments yet.