日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
淺談LINQtoACCESS簡(jiǎn)單實(shí)現(xiàn)

在LINQ to SQL里面都是用的DbConnection,而不是SQLConnection,這樣的話理論上可以支持所有的數(shù)據(jù),但對(duì)于一些數(shù)據(jù)庫(kù)的支持可能不是太好。例如分頁(yè),SQL Server 2000,SQL Server 2005,SQL Server 2008數(shù)據(jù),使用LINQ的代碼都不一樣,而ACCESS和SQL Server 2000比較接近,就可以直接使用SQL Server 2000Provider。查了一些資料看到,理論有那個(gè)數(shù)據(jù)庫(kù)provider,就可以支持這種數(shù)據(jù)庫(kù)。也看了dbLINQ 0.8支持不同數(shù)據(jù)庫(kù)的源碼,但自己能力有限不能寫(xiě)一個(gè)ACCESS的,還是用官方的吧。下邊說(shuō)一下方法。

創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)全網(wǎng)整合營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、橋東網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為橋東等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

其實(shí)他不太麻煩,只是改一下,*.designer.cs文件里的代碼。因?yàn)锳CCESS 不支持dbo,而LINQ to SQL里數(shù)據(jù)表前面都有dbo.的前綴, [Table(Name="dbo.wjk3")],將dbo.去掉,不然的話,會(huì)提示你找不到dbo數(shù)據(jù)庫(kù),這點(diǎn)上,自己走了不少?gòu)澛?。在public partial class DDataContext: System.Data.LINQ.DataContext上邊加上, [Provider(typeof(System.Data.LINQ.SQLClient.SQL Server 2000Provider))]設(shè)定為SQL Server 2000Provider,不然的話 LINQ 里面的first 不能使用,另外分頁(yè)也不能使用,因?yàn)樗J(rèn)的是SQL Server 2008Provider。

這一點(diǎn)很重要,到現(xiàn)在為止,基本上解決LINQ to ACCESS的使用,但還有一點(diǎn)問(wèn)題,從數(shù)據(jù)庫(kù)讀取一條記錄,修改后使用SubmitChanges()更新,提示錯(cuò)誤,不能修改,錯(cuò)誤內(nèi)容:找不到行或行已更改。這一點(diǎn)可以使用一些自定義方法來(lái)實(shí)現(xiàn)更新,使用ExecuteCommand()直接執(zhí)行更新SQL語(yǔ)句來(lái)實(shí)現(xiàn)。感覺(jué)LINQ to SQL的跟蹤,如果不適用SubmitChanges()更新的話,跟蹤也每太大的意義,實(shí)現(xiàn)跟蹤可能會(huì)降低系能,另外添加,刪除也依賴(lài)跟蹤,如果不使用跟蹤的話,還要擴(kuò)展添加,刪除的方法。

 
 
 
  1. public partial class dbgame  
  2.     {  
  3.         public IQueryable Find(TEntity obj) where TEntity : class  
  4.         {  
  5.             //獲得所有property的信息  
  6.             PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  7.             //構(gòu)造初始的query  
  8.             IQueryable query = this.GetTable().AsQueryable();  
  9.             //遍歷每個(gè)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, bool>> pred = Expression.Lambda, bool>>(filter, param);  
  33.                                 queryquery = query.Where(pred);  
  34.                             }  
  35.                         }  
  36.                     }  
  37.                 }  
  38.             }  
  39.             return query;  
  40.         }  
  41.         public void Update(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 query = this.GetTable().AsQueryable();  
  51.             //遍歷每個(gè)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("
    "+str+"
    ");  
  85.              this.ExecuteCommand(str);  
  86.             
  87.         }  
  88.         public void Insert(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 query = this.GetTable().AsQueryable();  
  99.             //遍歷每個(gè)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("
    " + str + "
    ");  
  133.             this.ExecuteCommand(str);  
  134.  
  135.         }  
  136.         public void Delete(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 query = this.GetTable().AsQueryable();  
  146.             //遍歷每個(gè)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("
    " + str + "
    ");  
  179.             this.ExecuteCommand(str);  
  180.  
  181.         }  
  182.         public IQueryable FindKey(object value) where TEntity : class  
  183.         {  
  184.             //獲得所有property的信息  
  185.             PropertyInfo[] properties = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  186.             //構(gòu)造初始的query  
  187.             IQueryable query = this.GetTable().AsQueryable();  
  188.             //遍歷每個(gè)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, bool>> pred = Expression.Lambda, bool>>(filter, param);  
  208.  
  209.                             queryquery = query.Where(pred);  
  210.                            break;  
  211.                         }  
  212.                     }  
  213.                 }  
  214.             }  
  215.             return query;  
  216.         }  
  217.     } 

沒(méi)有解決的問(wèn)題:

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

【編輯推薦】

  1. 使用LINQ查詢(xún)泛型字典Dictionary
  2. 淺析Linq to SQL更新數(shù)據(jù)時(shí)容易忽略的問(wèn)題
  3. 淺談LINQ to SQL集成數(shù)據(jù)庫(kù)語(yǔ)言?xún)?yōu)劣
  4. LINQ橫向?qū)Ρ萬(wàn)oreach方法
  5. 淺談LINQ如何插入刪除和更新數(shù)據(jù)庫(kù)記錄備注

新聞名稱(chēng):淺談LINQtoACCESS簡(jiǎn)單實(shí)現(xiàn)
本文地址:http://m.5511xx.com/article/djcojio.html