3

I have a project which I am developing with SQLite and SubSonic 3. I want to know that how can I avoid the App.config file altogether. How do I tell my program to use SQLite Data provider. I mean, how can I tell it this:

<DbProviderFactories>
    <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>

also can I add a connection string by code:

<add name="xyz" connectionString="Data Source=xyz.db" providerName="System.Data.SQLite"/>
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
Yogesh
  • 14,498
  • 6
  • 44
  • 69

2 Answers2

4

You can create a Provider manually by passing the connection string in:

var p = ProviderFactory.GetProvider("connectionstring", "System.Data.SqlClient");

Then you can use that Provider when you need to. This is ideal for IoC where you can setup a rule for SubSonic's IDataProvider:

ForRequestedType< IDataProvider >()
    .TheDefault.Is.ConstructedBy(x => 
        ProviderFactory.GetProvider("connectionstring", "System.Data.SqlClient"));

Gotta love IoC :)

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
1

A lot of libraries rely on the app.config, so you sooner or later are going to need it anyway. It is very central to the whole .NET platform. And what do you see as the problem?

You can easily provide your own connection string but I'm not so sure about loading a specific ADO.NET provider in code.

It depends on how you use your SQLite provider, but if it's classes are hardcoded into your code then I don't think you even need a DbProviderFactory.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Yes. I know and I have even used custom ConfigurationSections quite extensively. Actually, I wanted to drop config file for registry for this application of mine. But now I have decided to use it anyway. But still I will love to know that is this even possible? – Yogesh Oct 27 '09 at 18:23