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;
}
运行效果图:
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;
}
运行效果图:
相关文章推荐
- Linq学习第二天
- 【十天学会Linq to sql】第二天—–数据上下文
- Linq写的Dropdownlist用户控件,递归生成项
- thinking in java 第二天
- Linq to SQL语句之 Group By /Having和Exists/In/AnyAll/Contains
- 使用linq计算元素在列表中出现的次数c#代码
- 今天关于Linq,NHibernate等持久数据的理解
- Linq to Entities中无法构造实体或复杂类型
- andorid 第二天 hello kitty
- Java基础复习第二天
- 【寻工记】-上班第二天
- ASP.NET#LinqDataSource控件配置对象模型时遇到的问题
- Linq to SQL 表与表之间关联
- 黑马程序员——Objective-C 第二天课程学习总结
- LINQ: Building an IQueryable Provider - Part IV
- Linq tostring() issue
- LINQ: Building an IQueryable Provider - Part XI
- 8天入门wpf—— 第二天 xaml详解
- Linq To Sql, 为何继承就这么费劲?
- python执行等待程序直到第二天零点的方法