LinQ To Entity的增删改查
2010-05-27 11:58
190 查看
本示例程序采用Northwind 示例数据库,下面开始编写代码对Customers表进行增、删、改、查等等操作。
1.新增Customers记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
Customers customer = new Customers();
Random rm = new Random();
customer.CustomerID = "A" + rm.Next(9999).ToString();
customer.CompanyName = "EntLib.com Forum";
customer.Address = "http://www.EntLib.com";
myDb.AddToCustomers(customer);
int count = myDb.SaveChanges();
txtCustomerID.Text = customer.CustomerID;
}
2.更新Customers记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
var query = from customer in myDb.Customers
where customer.CustomerID == txtCustomerID.Text.Trim()
select customer;
foreach (var row in query)
{
row.CompanyName = "Updated Company Name";
}
myDb.SaveChanges();
}
3.删除Customers记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
var query = from customer in myDb.Customers
where customer.CustomerID == txtCustomerID.Text.Trim()
select customer;
foreach (var row in query)
{
myDb.DeleteObject(row);
}
myDb.SaveChanges();
}
4.查询Customers,返回所有记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
dataGridView1.DataSource = myDb.Customers;
}
1.新增Customers记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
Customers customer = new Customers();
Random rm = new Random();
customer.CustomerID = "A" + rm.Next(9999).ToString();
customer.CompanyName = "EntLib.com Forum";
customer.Address = "http://www.EntLib.com";
myDb.AddToCustomers(customer);
int count = myDb.SaveChanges();
txtCustomerID.Text = customer.CustomerID;
}
2.更新Customers记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
var query = from customer in myDb.Customers
where customer.CustomerID == txtCustomerID.Text.Trim()
select customer;
foreach (var row in query)
{
row.CompanyName = "Updated Company Name";
}
myDb.SaveChanges();
}
3.删除Customers记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
var query = from customer in myDb.Customers
where customer.CustomerID == txtCustomerID.Text.Trim()
select customer;
foreach (var row in query)
{
myDb.DeleteObject(row);
}
myDb.SaveChanges();
}
4.查询Customers,返回所有记录
using (NorthwindEntities myDb = new NorthwindEntities())
{
dataGridView1.DataSource = myDb.Customers;
}
相关文章推荐
- Linq To Entity 的增删改查(技术储备,怕忘了)
- LinQ To Entity的增删改查
- Linq To Entity 的增删改查(转)
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查(转)
- ADO.NET - Entity Data Model vs. LINQ To SQL
- LINQ to SQL(LINQ2SQL) vs. ADO.NET Entity Framework
- [新手入门]快速学习 ADO.NET Entity Framework系列文章 #3 -- LINQ-to-SQL、EntitySQL、查询产生器方法(Query builder)三种语法
- ADO.NET Entity Framework & LINQ to Entities - deel 1
- 转:LINQ to SQL(LINQ2SQL) vs. ADO.NET Entity Framework(ADOEF)-ccBoy版
- The entity cannot be constructed in a LINQ to Entit
- linq to Entity 实现 left join
- 比较LINQ to SQL Diagram 和Entity Data Model
- linq to entity 详解(查询)(转)
- VS2008下使用Linq To Entity的Skip().Take()分页查询时遇到数据结果不对的问题
- Linq与where实现查询(Linq to Entity)【IEnumerable与IEnumerator与IList】|自己实现foreach的功能
- Entity Framework vs. LINQ to SQL
- 关于linq to sql 或linq to entity 随机取数据
- linq to entity 的文集集锦