在LINQ to SQL里面都是用的DbConnection,而不是SQLConnection,这样的话理论上可以支持所有的数据,但对于一些数据库的支持可能不是太好。例如分页,SQL Server 2000,SQL Server 2005,SQL Server 2008数据,使用LINQ的代码都不一样,而ACCESS和SQL Server 2000比较接近,就可以直接使用SQL Server 2000Provider。查了一些资料看到,理论有那个数据库provider,就可以支持这种数据库。也看了dbLINQ 0.8支持不同数据库的源码,但自己能力有限不能写一个ACCESS的,还是用官方的吧。下边说一下方法。
其实他不太麻烦,只是改一下,*.designer.cs文件里的代码。因为ACCESS 不支持dbo,而LINQ to SQL里数据表前面都有dbo.的前缀, [Table(Name="dbo.wjk3")],将dbo.去掉,不然的话,会提示你找不到dbo数据库,这点上,自己走了不少弯路。在public partial class DDataContext: System.Data.LINQ.DataContext上边加上, [Provider(typeof(System.Data.LINQ.SQLClient.SQL Server 2000Provider))]设定为SQL Server 2000Provider,不然的话 LINQ 里面的first 不能使用,另外分页也不能使用,因为他默认的是SQL Server 2008Provider。
这一点很重要,到现在为止,基本上解决LINQ to ACCESS的使用,但还有一点问题,从数据库读取一条记录,修改后使用SubmitChanges()更新,提示错误,不能修改,错误内容:找不到行或行已更改。这一点可以使用一些自定义方法来实现更新,使用ExecuteCommand()直接执行更新SQL语句来实现。感觉LINQ to SQL的跟踪,如果不适用SubmitChanges()更新的话,跟踪也每太大的意义,实现跟踪可能会降低系能,另外添加,删除也依赖跟踪,如果不使用跟踪的话,还要扩展添加,删除的方法。
- public partial class dbgame
- {
- public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class
- {
- //获得所有property的信息
- PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //构造初始的query
- IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
- //遍历每个property
- foreach (PropertyInfo p in properties)
- {
- if (p != null)
- {
- Type t = p.PropertyType;
- //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
- if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
- || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
- || t == typeof(System.Data.Linq.Binary))
- {
- //如果不为null才算做条件
- if (p.GetValue(obj, null) != null)
- {
- if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)
- {
- }
- else
- {
- ParameterExpression param = Expression.Parameter(typeof(TEntity),"c");
- Expression right = Expression.Constant(p.GetValue(obj, null));
- Expression left = Expression.Property(param, p.Name);
- Expression filter = Expression.Equal(left, right);
- Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
- queryquery = query.Where(pred);
- }
- }
- }
- }
- }
- return query;
- }
- public void Update<TEntity>(TEntity obj) where TEntity : class
- {
- string str = "update " + typeof(TEntity).Name + " set ";
- string cols = "";
- string where="";
- //获得所有property的信息
- PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //构造初始的query
- IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
- //遍历每个property
- foreach (PropertyInfo p in properties)
- {
- if (p != null)
- {
- Type t = p.PropertyType;
- //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
- if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
- || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
- || t == typeof(System.Data.Linq.Binary))
- {
- //如果不为null才算做条件
- if (p.GetValue(obj, null) != null)
- {
- if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
- {
- where +=" where "+p.Name+"="+p.GetValue(obj,null);
- }
- else
- {
- if (t == typeof(System.Byte[]) || t == typeof(System.Int32) || t == typeof(Double) || t == typeof(float))
- cols += p.Name + "=" + p.GetValue(obj, null) + ",";
- else
- cols += p.Name + "='" + p.GetValue(obj, null) + "',";
- }
- }
- }
- }
- }
- str += cols.Substring(0,cols.Length-1) +where;
- HttpContext.Current.Response.Write("<br>"+str+"<br>");
- this.ExecuteCommand(str);
- }
- public void Insert<TEntity>(TEntity obj) where TEntity : class
- {
- string str = "insert into [" + typeof(TEntity).Name + "] (";
- string cols = "";
- string vals = "";
- string where = "";
- //获得所有property的信息
- PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //构造初始的query
- IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
- //遍历每个property
- foreach (PropertyInfo p in properties)
- {
- if (p != null)
- {
- Type t = p.PropertyType;
- //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
- if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
- || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
- || t == typeof(System.Data.Linq.Binary))
- {
- //如果不为null才算做条件
- if (p.GetValue(obj, null) != null)
- {
- //if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj,null))==0)
- //{
- //}
- //else
- //{
- cols += "["+p.Name + "],";
- if (t == typeof(System.Byte[]) || t == typeof(System.Int32) || t == typeof(Double) || t == typeof(float))
- vals += p.GetValue(obj, null) + ",";
- else
- vals +="'"+ p.GetValue(obj, null) + "',";
- // }
- }
- }
- }
- }
- str += cols.Substring(0, cols.Length - 1) + ") values (" + vals.Substring(0, vals.Length - 1) + ")";
- HttpContext.Current.Response.Write("<br>" + str + "<br>");
- this.ExecuteCommand(str);
- }
- public void Delete<TEntity>(TEntity obj) where TEntity : class
- {
- string str = "delete from [" + typeof(TEntity).Name+"] where ";
- string cols = "";
- string where = "";
- //获得所有property的信息
- PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //构造初始的query
- IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
- //遍历每个property
- foreach (PropertyInfo p in properties)
- {
- if (p != null)
- {
- Type t = p.PropertyType;
- //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
- if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
- || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
- || t == typeof(System.Data.Linq.Binary))
- {
- //如果不为null才算做条件
- if (p.GetValue(obj, null) != null)
- {
- if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)
- {
- }
- else
- {
- if (t == typeof(System.Byte[]) || t == typeof(System.Int32) || t == typeof(Double) || t == typeof(float))
- where +="["+p.Name+"]" + "=" + p.GetValue(obj, null) + " and ";
- else
- where += "[" + p.Name + "]" + "='" + p.GetValue(obj, null) + "' and ";
- }
- }
- }
- }
- }
- str +=where.Substring(0,where.Length-5);
- HttpContext.Current.Response.Write("<br>" + str + "<br>");
- this.ExecuteCommand(str);
- }
- public IQueryable<TEntity> FindKey<TEntity>(object value) where TEntity : class
- {
- //获得所有property的信息
- PropertyInfo[] properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //构造初始的query
- IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();
- //遍历每个property
- foreach (PropertyInfo p in properties)
- {
- if (p != null)
- {
- Type t = p.PropertyType;
- //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。
- if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])
- || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)
- || t == typeof(System.Data.Linq.Binary))
- {
- //如果不为null才算做条件
- if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)
- {
- ParameterExpression param = Expression.Parameter(typeof(TEntity), "d");
- Expression right = Expression.Constant(value);
- Expression left = Expression.Property(param, p.Name);
- Expression filter = Expression.Equal(left, right);
- Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);
- queryquery = query.Where(pred);
- break;
- }
- }
- }
- }
- return query;
- }
- }
没有解决的问题:
怎样能解决更新的时候能直接使用SubmitChanges();
【编辑推荐】