What ORM can I use for Access 2007 - 2010? I'm after WPF binding to the tables etc

前端 未结 4 992
抹茶落季
抹茶落季 2020-12-17 02:37

I\'ve a legacy database that all sites have, it describes specific content in a number of catagory/subcatagory/child item format. Until now, adding/editing the content is ei

4条回答
  •  情深已故
    2020-12-17 02:58

    Hate to resurrect an old thread but I recently did a WPF project using PetaPoco, a micro-ORM, with MS Access so I thought I'd share my implementation.

    To add MS Access support to PetaPoco, you just need to add a couple of bits of code:

    First add an AccessDatabaseType class. All of the DataBaseType classes are at the end of the PetaPoco.cs file. Just add the new class after SqlServerDatabaseType.

    class AccessDatabaseType : DatabaseType
    {
        public override object ExecuteInsert(Database db, IDbCommand cmd, string PrimaryKeyName)
        {               
            db.ExecuteNonQueryHelper(cmd);
            return db.ExecuteScalar("SELECT @@@IDENTITY AS NewID;");
        }
    
    }
    
    
    

    Next, modify PetaPoco.Internal.DatabaseType.Resolve() to support the AccessDatabaseType. (This code assumes you are using the Jet OLEDB provider)

    public static DatabaseType Resolve(string TypeName, string ProviderName)
    {
        //...
    
        if (ProviderName.IndexOf("Oledb", StringComparison.InvariantCultureIgnoreCase) >= 0)
            return Singleton.Instance;  
    
        // Assume SQL Server
        return Singleton.Instance;
    }
    

    Finally, to instantiate PetaPoco use this:

    Db = New PetaPoco.Database("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db.mdb", "System.Data.Oledb")
    

    Limitations:

    • PetaPoco assumes your primary keys are autonumber/identity fields. If you have a PK that's not an autonumber or you have a composite PK, you'll need to implement your own insert & save logic.
    • I didn't need paging in my application so I didn't implement it.

    提交回复
    热议问题