1.DLINQ总结
LinqtoSQL是微软Linq类的一部分,应该说Linq to SQL叫做DLinq
DLinq不等于Linq,Linq在.net项目开发过程中有特有的优势与缺点.
DLinq是对SqlServer的一种应用集,可以是框架级的.对SqlServer ORM的应用非常简单,不需要一行代码,一个XML文件的编写,而能实现1-1,1-N的映射,当然也能够提供CRUD功能,乃至存储过程,并发,锁,存储过程等等!
2.正确的使用DLINQ需要,熟悉.net3.0的几个新特性,它们都很简单,但非常有用.
2.1 隐士类型转换
2.2 匿名类型
2.3 扩展方法
2.4 自定义属性
2.5 对象初始化器
2.6 集合初始化器
2.7 Lambda表达式
2.8 查询方法
3.DataContext -System.Data.Linq的重要组成部分
翻译为数据上下文,用于将LINQ语言翻译成SQL,执行SQL和以日志形式进行操作
http://www.cnblogs.com/lovecherry/archive/2007/08/15/856977.html
3.1 DLINQ实体映射类-手动创建
如:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq.Mapping; namespace LinqDemo04_DataContext例子 { [Table(Name="Customers") ] public class Customer { [Column(IsPrimaryKey = true )] public string CustomerID { get; set; } [Column(Name = "ContactName")] public string Name { get; set; } [Column ] public string City { get; set; } } }
//很简单,多几个与定义Attribute而已
3.2使DataContext与实体类关联之数据库关联 (DSQL实体与SQL数据库关联)
方法1:DataContext构造方法中,关联数据库 Context ctx =new DataContext("Seriver=xxx;database=xxx;uid=xx;password=xx");
方法2:IDbConntion方法
using System.Data.SqlClient;
IDbconnection con= new SqlConnection ("server=xxx;database=Northwind;uid=xxx;pwd=xxx" );
DataContext ctx=new DataContext(con);
3.3强类型的Context
3.4日志功能
DataContext 实 例子 具有.Log属性,它是一个文件流StremWriter对象,可以向指定的文件中填写T-Sql执行语句及相关的操作信息!
我们只需要对其进行IO流操作就可以轻松使用,其功能对测试很有帮助!
如:
DataContext ctx=new DataContext("连接字符串 ");
Table<Customer> _Customers=ctx.GetTable<Customer>();
StreamWriter sw=new StreamWriter(Server.Mapth("log.txt"),true);//Append IO
ctx.Log=sw;
GridView1.DataSource=form in _Customers where c.CustomerID.StartsWith("A") select new {编号=c.CustomerID,顾客=c.Name,城市=c.City};
this.GridView1.DataBind();
sw.Close();//关闭IO
3.5探究查询
3.6执行查询
3.7创建数据库
3.8使用DbDataReader数据源
DataReader DataCommand SqlConntion 这写基础的ADO.NET对象是可以和DLinq一起使用的。
如:
Using System.Data.SqlClient;
var conn =new SqlConnection("连接字符串 ");
var ctx=new DataContext(conn);
var cmd=new SqlCommand("select * from customer where CustomerID like 'A%'",conn );
conn.Open();
var reader =cmd.ExecuteReader();
this.GridView1.DataSource=ctx.Translate<Customer>(reader);
this.GridView1.DataBind();
conn.Close();