一.引用命名空间
System.Data.Linq
System.Data.Linq.Mapping (需要在项目中先引用System.Data.Linq)
二.为数据库表创建实体类
Table(Name="Employees")] |
常用的属性声明,可以很好的描述表中字段的属性:
Name: 字段的名称
DbType: 字段的数据类型
IsDbGenerated: 是否自动生成
CanBeNull: 字段是否为空
Storage: 保存数据的类字段名
三.创建一个强类型的数据环境
我们的数据库系统可能会和多种类型的数据源打交道,所以应该使我们的程序有和数据源进行创建连接的能力。这里我们将创建的NorthwindDb类继承DataContext基类。DataContext类表示LINQ to SQL 框架的主入口点。
其中有相关的处理LINQ to SQL的类,方法等。
public class NorthwindDb:DataContext |
四.创建Web From
首先为GridView绑定数据
private void BindGridView(string criteria)
{
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
NorthwindDb db = new NorthwindDb(strConn);
IEnumerableresults;
if (criteria == string.Empty)
{
results=db.Employee.ToArray();
}
else
{
results = (from c in db.Employee
where c.FirstName.Contains(criteria)
select c).ToArray();
}
GridView1.DataSource = results;
GridView1.DataBind();
}
搜索按钮只需将参数传进去即可BindGridView(TextBox1.Text);
如果选中了GridView中的某一行,则需要将选中的数据绑定到DetailsView 中进行编辑使用。
private void BindDetailsView()
{
int employeeID = (int)GridView1.SelectedValue;
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
NorthwindDb db = new NorthwindDb(strConn);
var results = from emp in db.Employee
where emp.EmployeeID == employeeID
select emp;
DetailsView1.DataSource = results;
DetailsView1.DataBind();
}protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.SelectedIndex = e.NewSelectedIndex;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int employeeID = (int)GridView1.SelectedValue;
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
BindDetailsView();
}
接下来就是对数据的增删改操作:
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
NorthwindDb db = new NorthwindDb(strConn);
var results = from emp in db.Employee
where emp.EmployeeID == (int)DetailsView1.SelectedValue
select emp;
results.First().FirstName = ((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text;
results.First().LastName = ((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text;
db.SubmitChanges();
BindGridView(string.Empty);
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
NorthwindDb db = new NorthwindDb(strConn);
Employee emp = new Employee();
emp.FirstName = ((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text;
emp.LastName= ((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text;
db.Employee.InsertOnSubmit(emp);
db.SubmitChanges();
BindGridView(string.Empty);
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
NorthwindDb db = new NorthwindDb(strConn);
var results = db.Employee.Single(emp => emp.EmployeeID==(int)DetailsView1.SelectedValue);
db.Employee.DeleteOnSubmit(results);
db.SubmitChanges();
BindGridView(string.Empty);
}
更新和添加的操作差不多,只是作了些小的改动。
ok,这就是在ASP.NET中利用LINQ对数据的操作。
【编辑推荐】