Linq To Sql Standard Database Operations Inserts

本文介绍了使用LINQ到SQL技术插入记录到数据库的具体步骤,包括创建DataContext、实例化实体对象、将实体对象添加到相应的表集合以及调用SubmitChanges方法。

The first step to insert a record into a database is to create a DataContext. That is the first step for
every LINQ to SQL query.
Second, an entity object is instantiated from an entity class.
Third, that entity object is inserted into the appropriate table collection. And fourth, the SubmitChanges method is called on the DataContext.

1 Inserting a Record by Inserting an Entity Object into Table<T>

// 1. Create the DataContext.
Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");
// 2. Instantiate an entity object.
Customer cust =
new Customer
{
CustomerID 
= "LAWN",
CompanyName 
= "Lawn Wranglers",
ContactName 
= "Mr. Abe Henry",
ContactTitle 
= "Owner",
Address 
= "1017 Maple Leaf Way",
City 
= "Ft. Worth",
Region 
= "TX",
PostalCode 
= "76104",
Country 
= "USA",
Phone 
= "(800) MOW-LAWN",
Fax 
= "(800) MOW-LAWO"
};
// 3. Add the entity object to the Customers table.
db.Customers.InsertOnSubmit(cust);
// 4. Call the SubmitChanges method.
db.SubmitChanges();

// 5. Query the record.
Customer customer = db.Customers.Where(c => c.CustomerID == "LAWN").First();
Console.WriteLine(
"{0} - {1}", customer.CompanyName, customer.ContactName);
// This part of the code merely resets the database so the example can be
// run more than once.
Console.WriteLine("Deleting the added customer LAWN.");
db.Customers.DeleteOnSubmit(cust);
db.SubmitChanges();

2. Inserting a Record into the Northwind Database by Adding It to EntitySet<T>
Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");
Customer cust 
= (from c in db.Customers
where c.CustomerID == "LONEP"
select c).Single
<Customer>();
// Used to query record back out.
DateTime now = DateTime.Now;

Order order 
= new Order
{
CustomerID 
= cust.CustomerID,
EmployeeID 
= 4,
OrderDate 
= now,
RequiredDate 
= DateTime.Now.AddDays(7),
ShipVia 
= 3,
Freight 
= new Decimal(24.66),
ShipName 
= cust.CompanyName,
ShipAddress 
= cust.Address,
ShipCity 
= cust.City,
ShipRegion 
= cust.Region,
ShipPostalCode 
= cust.PostalCode,
ShipCountry 
= cust.Country
};
cust.Orders.Add(order);
db.SubmitChanges();
IEnumerable
<Order> orders =
db.Orders.Where(o 
=> o.CustomerID == "LONEP" && o.OrderDate.Value == now);
foreach (Order o in orders)
{
Console.WriteLine(
"{0} {1}", o.OrderDate, o.ShipName);
}
// This part of the code merely resets the database so the example can be
// run more than once.
db.Orders.DeleteOnSubmit(order);
db.SubmitChanges();

3. Adding Attached Records

Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");
Customer cust 
=
new Customer {
CustomerID 
= "LAWN",
CompanyName 
= "Lawn Wranglers",
ContactName 
= "Mr. Abe Henry",
ContactTitle 
= "Owner",
Address 
= "1017 Maple Leaf Way",
City 
= "Ft. Worth",
Region 
= "TX",
PostalCode 
= "76104",
Country 
= "USA",
Phone 
= "(800) MOW-LAWN",
Fax 
= "(800) MOW-LAWO",
Orders 
= {
new Order {
CustomerID 
= "LAWN",
EmployeeID 
= 4,
OrderDate 
= DateTime.Now,
RequiredDate 
= DateTime.Now.AddDays(7),
ShipVia 
= 3,
Freight 
= new Decimal(24.66),
ShipName 
= "Lawn Wranglers",
ShipAddress 
= "1017 Maple Leaf Way",
ShipCity 
= "Ft. Worth",
ShipRegion 
= "TX",
ShipPostalCode 
= "76104",
ShipCountry 
= "USA"
}
}
};

db.Customers.InsertOnSubmit(cust);
db.SubmitChanges();
Customer customer 
= db.Customers.Where(c => c.CustomerID == "LAWN").First();
Console.WriteLine(
"{0} - {1}", customer.CompanyName, customer.ContactName);
foreach (Order order in customer.Orders)
{
Console.WriteLine(
"{0} - {1}", order.CustomerID, order.OrderDate);
}
// This part of the code merely resets the database so the example can be
// run more than once.
db.Orders.DeleteOnSubmit(cust.Orders.First());
db.Customers.DeleteObSubmit(cust);
db.SubmitChanges();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值