程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 讓ADO.NET Entity Framework 支持ACCESS數據庫

讓ADO.NET Entity Framework 支持ACCESS數據庫

編輯:C#入門知識

如寫的不好請見諒,本人水平有限。

懶人第一步:

勤快第二步:

 上下文
    
     
     
       構造函數
    
         
         
         SqlDoorEntities1() : (,  
         
         SqlDoorEntities1( connectionString) : (connectionString,  
         
         SqlDoorEntities1(EntityConnection connection) : (connection, 
    
         分部方法
    
         
    
         ObjectSet 屬性
    
         
         
         ObjectSet<CmdMsg> ((_CmdMsg == = .CreateObjectSet<CmdMsg>( ObjectSet<CmdMsg> 
         
         ObjectSet<Door> ((_Door == = .CreateObjectSet<Door>( ObjectSet<Door> 
         
         ObjectSet<Manager> ((_Manager == = .CreateObjectSet<Manager>( ObjectSet<Manager> 
         
         ObjectSet<Users> ((_Users == = .CreateObjectSet<Users>( ObjectSet<Users>

         AddTo 方法
    
         
         
         .AddObject( 
         
         .AddObject( 
         
         .AddObject( 
         
         .AddObject(

擴展LINQ to SQL:使用Lambda Expression批量刪除數據

 :base("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
+AppDomain.CurrentDomain.BaseDirectory + "bin\\DataDoor.mdb")
 IEnumerable 屬性
         
         

         IEnumerable<CmdMsg> ((_CmdMsg == = .CreateObjectSet<CmdMsg>( IEnumerable<CmdMsg> 
         
         IEnumerable<Door> ((_Door == = .CreateObjectSet<Door>( IEnumerable<Door> 
         
         IEnumerable<Manager> ((_Manager == = .CreateObjectSet<Manager>( IEnumerable<Manager> 
         
         IEnumerable<Users> ((_Users == = .CreateObjectSet<Users>( IEnumerable<Users>
         AddTo 方法

         
         
         .AddObject( 
         
         .AddObject( 
         
         .AddObject( 
         
         .AddObject(

懶人第三步:

為了讓代碼和EF使用方法基本一致,所以不得不做一些工作讓我寫的類基本滿足項目需要。首先實現一個讓

=  ( i = ; i < conditionBuilder.Arguments.Length; i++ ce = (ce == =   (ce =  (ce   || ce  = .Format(  (ce = .Format( 

IEnumerable<TEntity> SelectAll<TEntity>()  TEntity : =  entitySetName = strSqlQuery = .Format(= SelectWhere<TEntity>

 IEnumerable<TEntity> Where<TEntity>(Expression<Func<TEntity, >> Func)  TEntity : =  entitySetName = strWhere = GetWhereString(Func).Replace( strSqlQuery = .Format(= SelectWhere<TEntity>

IEnumerable<TEntity> SelectWhere<TEntity>( strSqlQuery)  TEntity : = 
            Type TBase = ((TBase.BaseType) !=  (TBase.Name == ) = IsPCEH =  (TBase !=  && TBase.Name == )  IsPCEH = = TDefault.GetType().GetProperties(BindingFlags.Public |<TEntity> Records =  List<TEntity> entitySetName == = 數據庫查詢開始
                = 
                    = TValue += ( i = ; i < sqlReader.FieldCount; i++ strField =
                        PropertyInfo p = properties.Where(P => .Compare(P.Name, strField, ) ==  數據轉換
                          (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetInt16(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetInt32(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetInt64(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetString(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetDouble(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetFloat(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetDecimal(i),   (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetDateTime(i),  (!sqlReader.IsDBNull(i)) p.SetValue(TValue, sqlReader.GetValue(i), = 

        Dictionary<, Dictionary<, Dictionary<, []>>> m_ArrDetection =  Dictionary<, Dictionary<, Dictionary<, []>>>
 PropertyChangedEH(
             (!m_IsDetectionChange) 
            PropertyInfo[] properties = sender.GetType().GetProperties(BindingFlags.Public |
            PropertyInfo EntityKey =
             (EntityKey == ) 
             Table =
             MainKey = EntityKey.GetValue(sender,  (MainKey ==  || MainKey == ) 
             (!m_ArrDetection.ContainsKey(Table)) m_ArrDetection[Table] =  Dictionary<, Dictionary<, []>>
             (!m_ArrDetection[Table].ContainsKey(MainKey)) m_ArrDetection[Table][MainKey] =  Dictionary<, []>
             (e.PropertyName == MainKey) = properties.Where(P => .Compare(P.Name, e.PropertyName, ) == 
            m_ArrDetection[Table][MainKey][e.PropertyName] =  [] { p.GetValue(sender, 

  (Attribute attr  (attr = (Key.EntityKeyProperty ==    (PropertyInfo Info  (IsEntityKeyProperty(Info))  

 UID =<Door> Arr =  List<Door>
                 (SqlDoorEntities Database = 
                    IEnumerable<Door> Records = Database.Where<Door>(R => R.U_ID == (Door Record 

增加

 IEnumerable<TEntity> CreateObjectSet<TEntity>( entitySetName)  TEntity :   SelectAll<TEntity>

 

List<> m_ArrAdd =  List<>  AddObject( strName, 

  (m_ArrDel.Count >  (m_ArrAdd.Count >  (m_ArrDetection.Count > =  

其實也沒什麼,就是看看數組中哪個有了需要增刪改的 就處理下,接著寫添加所有的函數。

 ( O  

下面該實現我們的insert into 了 直接使用也是可以的 就不用使用糾結的SaveChanges()了。

  AddNew<TEntity>(TEntity TDefault)  TEntity : = TDefault.GetType().GetProperties(BindingFlags.Public |= (EntityKey == )   Exception( TabName = EntityValue =  strRows =  strValues =  Rows Values
             (PropertyInfo Info  ce = Info.GetValue(TDefault,  strLeft = strRight =  (ce ==   (ce = .Format(  (ce =  (ce =  (ce   || ce  = .Format( (strLeft === (strRight.Length == )  (strLeft ==  || strLeft == ) = strRows + strLeft + = strValues + strRight + 
             (strRows.Length <  || strValues.Length < )   Exception(= strRows.Remove(strRows.Length - = strValues.Remove(strValues.Length -  strSqlQuery = .Format(==  

函數中也沒什麼,就是注意一下不要生成SQL語句的時候,把主鍵信息也生成進去,一般情況主鍵大多是個自動增長的數字吧。還有就是不要把EntityObject的屬性的特有主鍵信息寫入到數據庫中。根據反射寫入數據庫。

還是先現實EF的刪除方法DeleteObject

  DeleteObject(= TDefault.GetType().GetProperties(BindingFlags.Public |= (EntityKey == )   Exception( EntityValue = EntityKey.GetValue(TDefault,  (EntityValue ==  || EntityValue == )   Exception( entitySetName = KeyName = (!m_ArrDel.ContainsKey(KeyName)) m_ArrDel.Add(KeyName, List<>.Format(

然後我們需要建立我們自己的列表

Dictionary<, List<>> m_ArrDel =  Dictionary<, List<>>();

 

實現刪除函數

  Delete<TEntity>= TDefault.GetType().GetProperties(BindingFlags.Public |= (EntityKey == )   Exception( EntityValue = EntityKey.GetValue(TDefault,  (EntityValue ==  || EntityValue == )   Exception( entitySetName = strSqlQuery = .Format(==  

更新

 <> ArrSqlText =  List<> (KeyValuePair<, Dictionary<, Dictionary<, []>>> TabKVP 
                 TabName = (KeyValuePair<, Dictionary<, []>> KeyKVP  strSet =  strMainKeyName =  把數據列出來例如: a=1,c="2"
                     (KeyValuePair<, []> ValueKVP  (strMainKeyName.Length == ) strMainKeyName = ValueKVP.Value[ Va = ValueKVP.Value[ strLeft = strRight =  根據值確認是否添加引號
                         (ValueKVP.Value ==   (Va = .Format(  (Va =  (Va   || Va  = .Format(
                         (strRight.Length == )  (strLeft ==  || strLeft == ) += strLeft +  + strRight + 
                     (strSet.Length < ) = strSet.Remove(strSet.Length - 
                     strSqlQuery = .Format( ( strSqlQuery ==  

當然我們還有直接把對象直接賦值拷貝的時候( a = b),這時候是偵測不到屬性的變化的,所以我們要另外一個函數來支持更新,就是讓他實現偵測到屬性的變化。

  CopyPropertiesFrom( destObject,  (destObject.GetType().Name != destObject.GetType().Name)   Exception(= destObject.GetType().GetProperties(BindingFlags.Public |= sourceObject.GetType().GetProperties(BindingFlags.Public |= (EntityKey == )   Exception( ( i = ; i < destProperties.Length; i++ (destProperties[i]==|| destProperties[i].Name == EntityKey.Name)  (destProperties[i].Name ==  || destProperties[i].Name == )  DstV = destProperties[i].GetValue(destObject, SrcV = sourceProperties[i].GetValue(sourceObject,  (SrcV == ) ;
                 (DstV.ToString() == SrcV.ToString()) 

顯示-添加-刪除-修改的例子代碼

 顯示用戶= <Users> Users =  List<Users>=  (SqlDoorEntities Database = <Users> Records = (Records.Count() >  (Users U 

         創建用戶= 
                 (SqlDoorEntities Database = <Users> Records = Database.Where<Users>(R => R.U_Number == (Records.Count() > ,  RedirectToAction(
 RedirectToAction(,  += 

         編輯用戶=  ActionResult EditUser(
                
                 (SqlDoorEntities Database = = Database.Where<Users>(R => R.U_ID ==
 Content(

         刪除用戶=  ActionResult DeleteUser(

                 (SqlDoorEntities Database = = Database.Where<Users>(R => R.U_ID == (User !=  RedirectToAction(

最後

http://files.cnblogs.com/hackdragon/EFToAccess.zip

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved