Useful Tip

Nov 22, 2012 at 9:47 PM
Edited Nov 22, 2012 at 9:51 PM

Hi All,

Just wanted to share a useful tip with anyone using this ORM.

I couldn't find a way to open a data store using a traditional connection string, only the direct use of an SQLite file name was available.

I remembered that in the base System.Data.SQLite assembly there was a class called

SQLiteConnectionStringBuilder

I've used this many times in the past for building connection strings, but it only just occurred to me that I could also use it to parse them.

With that in mind, I created myself a normal run of the mill bit of access code to get the connection string from my app.config file using the ConfigurationManager, then once I had my full normal connection string loaded, I created a new SQLiteConnectionStringBuilder

Once I had the connection string builder it was then just a simple matter of looking in the DataSource property to find the file name of the database file, and then passing this into a new "DataStore" object as the file name it expected.

For reference, here is the code I used to get the file name:

 

using System.Configuration;
using System.Data.SQLite;

namespace DataAccess
{
  public static class DataConnection
  {
    public static string ConnectionString { get { return ConfigurationManager.ConnectionStrings["localDataCacheConnection"].ConnectionString; } }
    public static string DatabaseFile
    {
      get
      {
        SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder(ConnectionString);
        return csb.DataSource;
      }
    }
  }
}

After that, i then just defined the connection string in my app.config as usual for an ADO.NET connection string.

 

<connectionStrings>
    <add connectionString="data source=versionOneDataCache.db" name="localDataCacheConnection" providerName="System.Data.SQLite" />
    </connectionStrings>
    

 

Hope you find this useful.

Shawty