淺談LINQ to ACCESS簡單實現(xiàn)
在LINQ to SQL里面都是用的DbConnection,而不是SQLConnection,這樣的話理論上可以支持所有的數(shù)據(jù),但對于一些數(shù)據(jù)庫的支持可能不是太好。例如分頁,SQL Server 2000,SQL Server 2005,SQL Server 2008數(shù)據(jù),使用LINQ的代碼都不一樣,而ACCESS和SQL Server 2000比較接近,就可以直接使用SQL Server 2000Provider。查了一些資料看到,理論有那個數(shù)據(jù)庫provider,就可以支持這種數(shù)據(jù)庫。也看了dbLINQ 0.8支持不同數(shù)據(jù)庫的源碼,但自己能力有限不能寫一個ACCESS的,還是用官方的吧。下邊說一下方法。
其實他不太麻煩,只是改一下,*.designer.cs文件里的代碼。因為ACCESS 不支持dbo,而LINQ to SQL里數(shù)據(jù)表前面都有dbo.的前綴, [Table(Name="dbo.wjk3")],將dbo.去掉,不然的話,會提示你找不到dbo數(shù)據(jù)庫,這點上,自己走了不少彎路。在public partial class DDataContext: System.Data.LINQ.DataContext上邊加上, [Provider(typeof(System.Data.LINQ.SQLClient.SQL Server 2000Provider))]設(shè)定為SQL Server 2000Provider,不然的話 LINQ 里面的first 不能使用,另外分頁也不能使用,因為他默認的是SQL Server 2008Provider。
這一點很重要,到現(xiàn)在為止,基本上解決LINQ to ACCESS的使用,但還有一點問題,從數(shù)據(jù)庫讀取一條記錄,修改后使用SubmitChanges()更新,提示錯誤,不能修改,錯誤內(nèi)容:找不到行或行已更改。這一點可以使用一些自定義方法來實現(xiàn)更新,使用ExecuteCommand()直接執(zhí)行更新SQL語句來實現(xiàn)。感覺LINQ to SQL的跟蹤,如果不適用SubmitChanges()更新的話,跟蹤也每太大的意義,實現(xiàn)跟蹤可能會降低系能,另外添加,刪除也依賴跟蹤,如果不使用跟蹤的話,還要擴展添加,刪除的方法。
- public partial class dbgame
- {
- public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class
- {
- //獲得所有property的信息
- PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- //構(gòu)造初始的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);
- //構(gòu)造初始的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);
- //構(gòu)造初始的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);
- //構(gòu)造初始的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);
- //構(gòu)造初始的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();
【編輯推薦】