Linq To Entity 的增删改查(技术储备,怕忘了)
2010-10-09 09:21
225 查看
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;
}
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技术二:Linq to XML及xml增删改查的不同实现方式
- LinQ To Entity的增删改查(转)
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- Linq To Entity 的增删改查(转)
- Linq to Entity中连接两个数据库时
- 把ORM封装成一个类(linq to entity)
- How to compare two strings with Linq to Entity
- linq to entity 获取项的集合总数
- linq to entity 调用存储过程(带返还结果集的存储过程)
- Linq与where实现查询(Linq to Entity)【IEnumerable与IEnumerator与IList】|自己实现foreach的功能
- Comparision Linq toSQL with ADO.net Entity Framework
- Linq To Xml操作XML增删改查
- ADO.Net Entity Framework Linq To Entities 语法功能汇编
- ADO.NET Entity Framework 学习初级篇3-- LINQ TO Entities
- linq to sql 学习(4)增删改查实例
- 给Linq to Sql里的entity加自定义属性小结
- LINQ to XML实现XML文档的增删改查