自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談LINQ to ACCESS簡單實現(xiàn)

開發(fā) 后端
為了節(jié)省服務(wù)器,網(wǎng)站的訪問量也不算太大,一直想用ACCESS數(shù)據(jù)庫,但苦于LINQ不支持ACCESS,在網(wǎng)上找了很多的方法,但覺的都不是太完美。在看了LINQ to ACCESS的源碼后,才發(fā)現(xiàn),linq to sql 可以用于ACCESS數(shù)據(jù)庫。

在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)跟蹤可能會降低系能,另外添加,刪除也依賴跟蹤,如果不使用跟蹤的話,還要擴展添加,刪除的方法。

  1. public partial class dbgame  
  2.     {  
  3.         public IQueryable<TEntity> Find<TEntity>(TEntity obj) where TEntity : class  
  4.         {  
  5.             //獲得所有property的信息  
  6.             PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  7.             //構(gòu)造初始的query  
  8.             IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();  
  9.             //遍歷每個property  
  10.             foreach (PropertyInfo p in properties)  
  11.             {  
  12.                 if (p != null)  
  13.                 {  
  14.                     Type t = p.PropertyType;  
  15.                     //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。  
  16.                     if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])  
  17.                       || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)  
  18.                       || t == typeof(System.Data.Linq.Binary))  
  19.                     {  
  20.                         //如果不為null才算做條件  
  21.                         if (p.GetValue(obj, null) != null)  
  22.                         {  
  23.                             if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)  
  24.                             {  
  25.                             }  
  26.                             else  
  27.                             {  
  28.                                 ParameterExpression param = Expression.Parameter(typeof(TEntity),"c");  
  29.                                 Expression right = Expression.Constant(p.GetValue(obj, null));  
  30.                                 Expression left = Expression.Property(param, p.Name);  
  31.                                 Expression filter = Expression.Equal(left, right);  
  32.                                 Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);  
  33.                                 queryquery = query.Where(pred);  
  34.                             }  
  35.                         }  
  36.                     }  
  37.                 }  
  38.             }  
  39.             return query;  
  40.         }  
  41.         public void Update<TEntity>(TEntity obj) where TEntity : class  
  42.         {  
  43.             string str = "update  " + typeof(TEntity).Name + " set ";  
  44.             string cols = "";  
  45.             string where="";  
  46.             //獲得所有property的信息  
  47.             PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  48.             //構(gòu)造初始的query  
  49.  
  50.             IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();  
  51.             //遍歷每個property  
  52.             foreach (PropertyInfo p in properties)  
  53.             {  
  54.                 if (p != null)  
  55.                 {  
  56.                     Type t = p.PropertyType;  
  57.                     //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。  
  58.                     if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])  
  59.                       || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)  
  60.                       || t == typeof(System.Data.Linq.Binary))  
  61.                     {  
  62.                         //如果不為null才算做條件  
  63.  
  64.                         if (p.GetValue(obj, null) != null)  
  65.                         {  
  66.                             if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)  
  67.                             {  
  68.                                 where +=" where "+p.Name+"="+p.GetValue(obj,null);  
  69.                             }  
  70.                             else  
  71.                             {  
  72.                                  
  73.                                 if (t == typeof(System.Byte[]) || t == typeof(System.Int32) || t == typeof(Double) || t == typeof(float))  
  74.                                     cols += p.Name + "=" + p.GetValue(obj, null) + ",";  
  75.                                 else  
  76.                                     cols += p.Name + "='" + p.GetValue(obj, null) + "',";  
  77.                             }  
  78.                         }  
  79.                     }  
  80.                 }  
  81.             }  
  82.  
  83.             str += cols.Substring(0,cols.Length-1) +where;  
  84.             HttpContext.Current.Response.Write("<br>"+str+"<br>");  
  85.              this.ExecuteCommand(str);  
  86.             
  87.         }  
  88.         public void Insert<TEntity>(TEntity obj) where TEntity : class  
  89.         {  
  90.             string str = "insert into [" + typeof(TEntity).Name + "] (";  
  91.             string cols = "";  
  92.             string vals = "";  
  93.             string where = "";  
  94.             //獲得所有property的信息  
  95.             PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  96.             //構(gòu)造初始的query  
  97.  
  98.             IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();  
  99.             //遍歷每個property  
  100.             foreach (PropertyInfo p in properties)  
  101.             {  
  102.                 if (p != null)  
  103.                 {  
  104.                     Type t = p.PropertyType;  
  105.                     //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。  
  106.                     if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])  
  107.                       || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)  
  108.                       || t == typeof(System.Data.Linq.Binary))  
  109.                     {  
  110.                         //如果不為null才算做條件  
  111.  
  112.                         if (p.GetValue(obj, null) != null)  
  113.                         {  
  114.                             //if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj,null))==0)  
  115.                             //{  
  116.                                   
  117.                             //}  
  118.                             //else  
  119.                             //{  
  120.                                 cols += "["+p.Name + "],";  
  121.                                 if (t == typeof(System.Byte[]) || t == typeof(System.Int32) || t == typeof(Double) || t == typeof(float))  
  122.                                     vals += p.GetValue(obj, null) + ",";  
  123.                                 else  
  124.                                     vals +="'"+ p.GetValue(obj, null) + "',";  
  125.                            // }  
  126.                         }  
  127.                     }  
  128.                 }  
  129.             }  
  130.  
  131.             str += cols.Substring(0, cols.Length - 1) + ") values (" + vals.Substring(0, vals.Length - 1) + ")";  
  132.             HttpContext.Current.Response.Write("<br>" + str + "<br>");  
  133.             this.ExecuteCommand(str);  
  134.  
  135.         }  
  136.         public void Delete<TEntity>(TEntity obj) where TEntity : class  
  137.         {  
  138.             string str = "delete from [" + typeof(TEntity).Name+"] where ";  
  139.             string cols = "";  
  140.             string where = "";  
  141.             //獲得所有property的信息  
  142.             PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  143.             //構(gòu)造初始的query  
  144.  
  145.             IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();  
  146.             //遍歷每個property  
  147.             foreach (PropertyInfo p in properties)  
  148.             {  
  149.                 if (p != null)  
  150.                 {  
  151.                     Type t = p.PropertyType;  
  152.                     //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。  
  153.                     if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])  
  154.                       || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)  
  155.                       || t == typeof(System.Data.Linq.Binary))  
  156.                     {  
  157.                         //如果不為null才算做條件  
  158.  
  159.                         if (p.GetValue(obj, null) != null)  
  160.                         {  
  161.                             if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey && Convert.ToInt32(p.GetValue(obj, null)) == 0)  
  162.                             {  
  163.  
  164.                              }  
  165.                             else  
  166.                             {  
  167.                                 if (t == typeof(System.Byte[]) || t == typeof(System.Int32) || t == typeof(Double) || t == typeof(float))  
  168.                                     where +="["+p.Name+"]" + "=" + p.GetValue(obj, null) + " and ";  
  169.                                 else  
  170.                                     where += "[" + p.Name + "]" + "='" + p.GetValue(obj, null) + "' and ";  
  171.                             }  
  172.                         }  
  173.                     }  
  174.                 }  
  175.             }  
  176.  
  177.             str +=where.Substring(0,where.Length-5);  
  178.             HttpContext.Current.Response.Write("<br>" + str + "<br>");  
  179.             this.ExecuteCommand(str);  
  180.  
  181.         }  
  182.         public IQueryable<TEntity> FindKey<TEntity>(object value) where TEntity : class  
  183.         {  
  184.             //獲得所有property的信息  
  185.             PropertyInfo[] properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  186.             //構(gòu)造初始的query  
  187.             IQueryable<TEntity> query = this.GetTable<TEntity>().AsQueryable<TEntity>();  
  188.             //遍歷每個property  
  189.             foreach (PropertyInfo p in properties)  
  190.             {  
  191.                 if (p != null)  
  192.                 {  
  193.                     Type t = p.PropertyType;  
  194.                     //加入object,Binary,和XDocument, 支持sql_variant,imager 和xml等的影射。  
  195.                     if (t.IsValueType || t == typeof(string) || t == typeof(System.Byte[])  
  196.                       || t == typeof(object) || t == typeof(System.Xml.Linq.XDocument)  
  197.                       || t == typeof(System.Data.Linq.Binary))  
  198.                     {  
  199.                         //如果不為null才算做條件  
  200.                         if (((ColumnAttribute)(p.GetCustomAttributes(typeof(ColumnAttribute), true)[0])).IsPrimaryKey)  
  201.                         {  
  202.                             ParameterExpression param = Expression.Parameter(typeof(TEntity), "d");  
  203.                             Expression right = Expression.Constant(value);  
  204.                             Expression left = Expression.Property(param, p.Name);  
  205.                             Expression filter = Expression.Equal(left, right);  
  206.  
  207.                             Expression<Func<TEntity, bool>> pred = Expression.Lambda<Func<TEntity, bool>>(filter, param);  
  208.  
  209.                             queryquery = query.Where(pred);  
  210.                            break;  
  211.                         }  
  212.                     }  
  213.                 }  
  214.             }  
  215.             return query;  
  216.         }  
  217.     } 

沒有解決的問題:

怎樣能解決更新的時候能直接使用SubmitChanges();

【編輯推薦】

  1. 使用LINQ查詢泛型字典Dictionary
  2. 淺析Linq to SQL更新數(shù)據(jù)時容易忽略的問題
  3. 淺談LINQ to SQL集成數(shù)據(jù)庫語言優(yōu)劣
  4. LINQ橫向?qū)Ρ萬oreach方法
  5. 淺談LINQ如何插入刪除和更新數(shù)據(jù)庫記錄備注
責(zé)任編輯:彭凡 來源: cnblogs
相關(guān)推薦

2009-09-08 10:03:13

Linq查詢Acces

2009-09-08 16:55:01

Linq實現(xiàn)XML轉(zhuǎn)換

2009-09-17 09:24:57

Linq實現(xiàn)分頁

2009-09-15 15:18:40

Linq連接查詢

2009-09-15 16:31:15

LINQ Custom

2009-09-09 15:44:22

Linq DataCo

2009-09-15 11:34:47

Linq多條件查詢

2009-09-07 17:32:14

LINQ檢索數(shù)據(jù)

2009-09-08 15:19:52

Linq Where操

2009-09-14 09:49:08

Linq擴展函數(shù)

2009-09-16 11:15:52

Linq聯(lián)接數(shù)據(jù)

2009-09-11 11:25:35

LINQ函數(shù)集合

2009-09-10 11:29:00

LINQ to SQL

2009-09-10 15:45:07

Linq使用Selec

2009-09-17 09:57:08

linq創(chuàng)建數(shù)據(jù)庫

2009-09-17 10:40:23

linq存儲過程

2009-09-15 11:08:01

LinQ調(diào)用存儲過程

2009-09-09 11:07:52

LINQ to SQL

2009-09-14 15:45:28

LINQ刪除XML節(jié)點

2009-09-18 16:32:51

Linq委托實例化
點贊
收藏

51CTO技術(shù)棧公眾號