Linq To Sql Standard Database Operations Inserts
2008-10-23 16:21
267 查看
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();
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();
相关文章推荐
- LINQ to SQL (Part 3 - Querying our Database)
- 【转载】LINQ to SQL (Part 3 - Querying our Database)
- 例子:Database - Linq to sql
- ScottGu之博客翻译-LINQ to SQL第四部分,更新数据库 LINQ to SQL (Part 4 - Updating our Database)
- 【转载】LINQ to SQL (Part 4 - Updating our Database)
- ScottGu博客之翻译-Linq to Sql第7部分--用存储过程更新数据库--LINQ to SQL (Part 7 - Updating our Database using Stored Procedures)
- [Database] SqlServer: Linq to Sql THE DATACONTEXT Change Processing
- SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.
- ScottGu之博客翻译-LINQ to SQL第三部分,查询数据库 (Part 3 - Querying our Database)
- LINQ to SQL Automatic database synchronization
- LINQ to SQL系列Part 7 - Updating our Database using Stored Procedures
- LINQ 笔记 - LINQ to SQL 基本数据操作
- Linq to sql 两种更新
- LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL
- 一步一步学Linq to sql(三):增删查改
- linq to sql 应用存储过程
- 去除Linq to Sql 查询结果中的空格
- 关于Linq to sql的一个更新问题
- Sql to Linq 小工具
- LINQ体验(15)——LINQ to SQL语句之用户定义函数