如何使用SQLite ADO.net数据提供程序?

lmvvr0a8  于 2023-05-07  发布在  SQLite
关注(0)|答案(2)|浏览(224)

我有a .NET 4.5 application,它已经启动并运行。此应用程序可以连接到SQL Server和MS Access数据库文件。
现在,我想使用一个SQLite数据库。
因此,我ADO.net从this link下载了SQLite www.example.com 4.5数据提供程序(sqlite-netFx 45-setup-x64-2012-1.0.117.0.exe),并将其安装在我的笔记本电脑上。
我的连接字符串如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="assemblies" type="Simple.Framework.AssembliesConfigurationSection, Simple.Framework"/>
  </configSections>

  <connectionStrings>
    <!--<add name="MyMSAccessConnStr" 
         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DB\access_db_file.mdb;Persist Security Info=False" 
         providerName="System.Data.OleDb"/>-->
    <!--<add name="MySQLServerConnStr" connectionString="Data Source=.\sqlexpress;Initial Catalog=gre;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>-->
    <add name="MySQLiteConnStr" connectionString="Data Source=C:\DB\sqlite_db_file.db;Version=3;"
         providerName="System.Data.SQLite"/>
  </connectionStrings>

  <startup>
    
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>

</configuration>

在上面的例子中,MyMSAccessConnStrMySqlServerConnStr已经可以正常工作了。
然而,SQLite连接不起作用。它给出以下错误:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Unable to find the requested .Net Framework Data Provider.  
It may not be installed.

我在GAC中也没有看到System.Data.Sqlite
如何解决此问题?

k2fxgqgv

k2fxgqgv1#

发生此错误的原因是您的系统不知道System.Data.SQLite ADO.Net数据库访问接口。
您可以自己在GAC中安装它,并在machine.config中声明它,如下所述:Obtaining a DbProviderFactory
但是你don't need to install it in the GAC

  • app.config中声明如下:
<configuration>
      <connectionStrings>
          <add name="PTSystemMSAccess" connectionString="Data Source=c:\mypath\my.db" providerName="System.Data.SQLite"/>
      </connectionStrings>

      <system.data>
          <DbProviderFactories>
              <remove invariant="System.Data.SQLite"/>
              <add name="SQLite Data Provider"
                  invariant="System.Data.SQLite"
                  description=".Net Framework Data Provider for SQLite"
                  type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
          </DbProviderFactories>
      </system.data>

  </configuration>
  • 只需将System.Data.SQLite.dllSQLite.Interop.dll复制到QuizApplication.exe文件旁边即可。
8fsztsew

8fsztsew2#

确保在<system.data>配置中包含该元素:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="assemblies" type="Simple.Framework.AssembliesConfigurationSection, Simple.Framework"/>
  </configSections>

  <connectionStrings>
    <!--<add name="MyMSAccessConnStr" 
         connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DB\access_db_file.mdb;Persist Security Info=False" 
         providerName="System.Data.OleDb"/>-->
    <!--<add name="MySQLServerConnStr" connectionString="Data Source=.\sqlexpress;Initial Catalog=gre;Integrated Security=True" 
         providerName="System.Data.SqlClient"/>-->
    <add name="MySQLiteConnStr" connectionString="Data Source=C:\DB\sqlite_db_file.db;Version=3;"
         providerName="System.Data.SQLite"/>
  </connectionStrings>

  <startup>
    
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>

  <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SQLite"/>
            <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
        </DbProviderFactories>
  </system.data>

</configuration>

另外,请确保在构建/调试/发布时将SQLite dll复制到输出目录(通常是bin文件夹):Example

相关问题