开发者

Building a relative path connection string to access a SQLite database in c#

I’m currently working in c# with a SQLite database file (data.db3) which is located in the application directory. During development, an absolute path has been used and it worked fine so far. Now I’m trying to access the database by using a relative path, but that fails because of a possibly wrong connection string. The following connection string works fine and was automatically created by the ADO.Net framework.

<connectionStrings>
<add name="dataEntities" 
 connectionString="metadata=res://*/DataEntities.csdl|res://*/DataEntities.ssdl|res://*/DataEntities.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;C:\Projekte\DataProvider\data.db3&quot;;datetimeformat=Ticks'" 
 providerName="System.Data.EntityClient" />
</connectionStrings>

Now I tried the following to access the database using a relative path (all fails):

  1. dataContext = new dataEntities("Data Sour开发者_Python百科ce=data.db3");
  2. dataContext = new dataEntities("Data Source=.\\data.db3");
  3. dataContext = new dataEntities("Data Source=data.db3;Version=3;DateTimeFormat=Ticks;");
  4. dataContext = new dataEntities("metadata=res://*/DataEntities.csdl|res://*/DataEntities.ssdl|res://*/DataEntities.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;data.db3&quot;;datetimeformat=Ticks'" providerName="System.Data.EntityClient");

Created by the ADO.Net framework:

public partial class dataEntities : ObjectContext
{

    public dataEntities() : base("name=dataEntities", "dataEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    public dataEntities(string connectionString) : base(connectionString, "dataEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

 /// ……

}


You might want to consider using the EntityConnectionStringBuilder class

Which will simplify at least isolating the connection string down to just the SQL part.

string baseFolder = AppDomain.CurrentDomain.BaseDirectory;

string sqlLiteConnectionString = string.Format(
  "data source=\"{0}\";datetimeformat=Ticks", 
  Path.Combine(baseFolder, "data.db3"));

var entityConnectionString = new EntityConnectionStringBuilder
{
  Metadata = "res://*",
  Provider = "System.Data.EntityClient",
  ProviderConnectionString = sqlLiteConnectionString,
}.ConnectionString;

var entities = new dataEntities(entityConnectionString);


The problem is NOT relative versus absolute path but the fact that you are trying to write in the application directory which is prohibited by newer Windows version for security reasons...

So this more an issue of permission/rights - depending on your OS (i.e. Windows 7...) and the user your running the app with (i.a. Administrator?) for security reasons you are not allowed to write in the application directory... if you need someplace with read+write you should use http://msdn.microsoft.com/de-de/library/system.windows.forms.application.userappdatapath.aspx

Just check whether the db is in that path -if not copy it there- and use it there...

Other locations could be ApplicationData/CommonApplicationData/LocalApplicationData from http://msdn.microsoft.com/de-de/library/14tx8hby.aspx and http://msdn.microsoft.com/de-de/library/system.environment.specialfolder.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜