打开sqlite连接时出错

时间:2015-05-22 08:53:04

标签: c# sqlite

ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["test"];
SQLiteConnection.CreateFile("MyDatabase.sqlite");
var dbFact = DbProviderFactories.GetFactory(connectionString.ProviderName);

using (var conn = dbFact.CreateConnection())
{
    conn.Open(); // Exception gets thrown here
}

每当我执行此代码时,我都会遇到以下异常:|

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

Additional information: Invalid ConnectionString format, cannot parse: string value to split cannot be null

这是我的app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <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>
  <connectionStrings>
    <add name="test" connectionString="Data Source=MyDatabase.sqlite;Version=3" providerName="System.Data.SQLite"/>
  </connectionStrings>
</configuration>

你能帮我解决这个问题,谢谢你提前。

1 个答案:

答案 0 :(得分:0)

事实证明我需要在打开它之前显式传递连接中的连接字符串:

ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["test"];
SQLiteConnection.CreateFile("MyDatabase.sqlite");
var dbFact = DbProviderFactories.GetFactory(connectionString.ProviderName);

using (var conn = dbFact.CreateConnection())
{
    conn.ConnectionString = connectionString.ConnectionString;
    conn.Open(); // Exception gets thrown here
}