您的位置:首页 > 其它

Linq 第二天

2008-12-08 20:18 267 查看
大家知道在开发数据时,数据一般是关系型数据,然而数据和对象是什么关系?linq就主要是解决数据不等对象而产生。有了Linq数据和对象之间就可以有一个一一对应的关系了。而且这些是可以根据数据库生成这种影射的代码,也可以根据影射代码生成数据库。就是说,数据库和影射代码实现了相互转化。

1、先添加Linq to SQL 类,命名为Northwind.dbml,然后再该类添加一数据表Customers

如图:


从图的Northwind.designer.cs文件可以看到

[System.Data.Linq.Mapping.DatabaseAttribute(Name="Northwind")]

public partial class NorthwindDataContext : System.Data.Linq.DataContext

{

[Table(Name="dbo.Customers")]

public partial class Customers : INotifyPropertyChanging, INotifyPropertyChanged

{

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

private string _CustomerID;

可以看到NorthwindDataContext是必须从DataContext 类继承,这样就获得Linq的支持。

2、添加一窗体Form1,然后在窗体添加dataGridView1,后如代码如下:

private void Form1_Load(object sender, EventArgs e)

{

NorthwindDataContext db = new NorthwindDataContext();

var c = from p in db.Customers

select p;

dataGridView1.DataSource = c;

}

运行效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: