linq学习笔记(1) -- 面向对象的查询,删除,更新,新增记录
即将开始一个新项目开发,使用VS2008 + Sql Server 2005开发,可能要引入linq,打算从今天开始学习linq,应该还不算晚~![]()
之前一直在VS2005下面做开发,对于园子里的linq学习潮有关注,但是感觉手头没有项目实践,所以只是看没有去实践。
园子里面webabcd的步步为营VS 2008 + .NET 3.5系列文章索引 感觉不错,俺打算吸收了
,根据自己的体会转成自己的开发经验,以便项目开发过程中作为开发笔记备查。
开门见山,首先是Linq的面向对象的查询
1)新建一个网站/Web Application(大废话)
2)在网站内添加一个LINQ to SQL 类,名称为Northwind.dbml,打开服务器资源管理器,展开Northwind数据库,将表Customers拖入到Northwind.dbml,然后保存。
3)在Default.aspx里面添加一个GridView数据控件,名称为gvSelLinQ,在页面后台类文件写入GirdView的数据绑定,关键代码如下:
1
private void BindGrid()2
{3
NorthwindDataContext northwind = new NorthwindDataContext();4
5
//如果你喜欢类似Sql语句那种写法,可以象注释这样写6
//var c = from t in northwind.Customers7
// where t.Country == "USA"8
// select t; 9
10
var c = northwind.Customers.Where(t => t.Country == "USA").ToList<Customers>();11

12
gvSelLinQ.DataSource = c;13
gvSelLinQ.DataBind();14
}这样,查询初级版本就这么完了~嗯嗯
接下来是删除了,沿用刚才那个网站和dbml文件
1)沿用刚才的Default.aspx,在页面上添加一个名为drpCustomerID的Dropdownlist下拉控件,一个名称为btnDelete的Button按钮控件,在后台页面写入方法分别为下拉控件绑定数据,Button控件提供Click事件响应。
drpCustomerID控件的数据绑定方法如下:
1
private void BindDDL()2
{3
NorthwindDataContext northwind = new NorthwindDataContext();4
//如果你喜欢类似Sql语句那种写法,可以如注释代码,自己写查询语句5
//var c = from t in northwind.Customers 6
// select t.CustomerID;7
var c = northwind.Customers.Select(t => t.CustomerID);8
drpCustomerID.DataSource = c;9
drpCustomerID.DataBind();10
}btnDelete控件的Click事件的方法如下:
1
protected void btnDelete_Click(object sender, EventArgs e)2
{3
string customerID = string.Empty;4
//获取要删除的CustomerID5
customerID = drpCustomerID.SelectedValue.ToString().Trim();6
//实例化NorthwindDataContext7
NorthwindDataContext northwind = new NorthwindDataContext();8

9
//使用Single查询操作符获取指定的Customers对象10
Customers customer = northwind.Customers.Single(p => p.CustomerID == customerID);11

12
//使用DeleteOnSubmit()方法删除NorthwindDataContext对象的Customers集合中的指定Customer对象13
northwind.Customers.DeleteOnSubmit(customer);14
15
//提交此次操作,生成并执行SQL命令16
northwind.SubmitChanges();17

18
//重新绑定DropDownList和GridView19
BindDDL();20
BindGrid(); //GridViewd的数据绑定就是上文提到的方法21
}第三步是更新了,沿用刚才那个网站和dbml文件
1)沿用刚才的Default.aspx,在页面上添加一个名为txtAddress的TextBox输入控件,一个名称为btnUpdate的Button按钮控件,在后台页面写入方法为Button控件提供Click事件响应。
1
protected void btnUpdate_Click(object sender, EventArgs e)2
{3
string customerID = string.Empty, address = string.Empty;4
5
//获取更新的内容6
customerID = drpCustomerID.SelectedValue.ToString().Trim();7
address = txtAddress.Text.Trim();8

9
//实例化NorthwindDataContext10
NorthwindDataContext northwind = new NorthwindDataContext();11

12
//获取Customers实体集中准备更新的那项实体13
Customers customer = northwind.Customers.Single(p => p.CustomerID == customerID);14

15
//为更新字段进行赋值16
customer.Address = address; 17

18
//提交更新操作19
northwind.SubmitChanges();20

21
//重新绑定DropDownList和GridView22
BindDDL();23
BindGrid();24
}第四步是新增,依然沿用刚才那个网站和dbml文件
1)沿用刚才的Default.aspx,在页面上使用原有的名为名为txtAddress的TextBox输入控件,再添加两个TextBox输入控件,名称分别是txtCustomerID,txtCompanyName,然后再添加一个Button按钮控件,名称是btnAdd,在后台页面写入方法为Button控件提供Click事件响应。
1
protected void btnAdd_Click(object sender, EventArgs e)2
{3
string customerID = string.Empty, address = string.Empty, companyName = string.Empty;4
//获取新增数据信息5
customerID = txtCustomerID.Text.Trim();6
companyName = txtCompanyName.Text.Trim();7
address = txtAddress.Text.Trim();8

9
//实例化NorthwindDataContext10
NorthwindDataContext northwind = new NorthwindDataContext();11
12
//实例化Customers13
Customers customer = new Customers();14
15
//字段赋值16
customer.CustomerID = customerID;17
customer.CompanyName = companyName;18
customer.Address = address;19

20
//使用InsertOnSubmit()方法新增NorthwindDataContext对象的Customers集合中的指定Customer对象 21
northwind.Customers.InsertOnSubmit(customer);22

23
//提交当前操作24
northwind.SubmitChanges();25

26
//重新绑定GridView27
BindGrid();28
}好的,现在使用Linq完成面向对象的查询,删除,更新,新增四步全部完成,Linq入门初级第一步完成~


浙公网安备 33010602011771号