PetaPOCO with MS Access database

时间:2016-01-26 05:54:58

标签: c# ms-access petapoco

I'm trying to test PetaPOCO with MS Access database.

Connection string in web.config

<add name="ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Data.mdb; Persist Security Info=False" providerName="System.Data.Oledb" />

Accessing MS Access database-

var db = new PetaPoco.Database("ConString"); //throws exception here
var rows = db.Query<Model>("SELECT * FROM Table");

Exception thrown-

"Could not match `System.Data.Oledb` to a provider.Parameter name: providerName"

Is there any way to do it? If yes, how?

3 个答案:

答案 0 :(得分:2)

从PetaPoco 5.1.127或更高版本开始,支持MS Access支持,不需要自定义数据库提供程序。

示例配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="msaccess" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Databases\MSAccess\petapoco.accdb" providerName="OleDb"/>
  </connectionStrings>
</configuration>

流畅的配置

var builder = DatabaseConfiguration.Build().UsingConnectionName("MyConnection");

var db = builder.Create();

构造函数配置

var db = new Database("MyConnection");

答案 1 :(得分:1)

正如他们的文档所述&#34;适用于SQL Server,SQL Server CE,SQLite,MySQL,MariaDB和PostgreSQL。 (Oracle支持但没有集成测试)。&#34;,它不支持MS Access

要使用它,您需要编写自己的提供程序。可以找到提供商的样本here

答案 2 :(得分:0)

我认为你真正想要的是这个

using System.Configuration;

var db = new PetaPoco.Database(ConfigurationManager.AppSettings["ConString"]);