Configure using app.config

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

Since Microsoft has released FW 2.0 and the <connectionStrings> section of configuration file is available, it should be used. This configuration method is preferable. The following method demonstrates this ability.

OpenConfig1FW2.cs
using System;
using System.Data;

using NUnit.Framework;

using BLToolkit.Data;

namespace HowTo.Data
{
    [TestFixture]
    public class OpenConfig1FW2
    {
        [Test]
        public void FW2Configuration()
        {
            // <connectionString> section configuration supported in FW 2.0+.
            //
            using (DbManager db = new DbManager("DemoConnection"))
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }
    }
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add
            name             = "DemoConnection"
            connectionString = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"
            providerName     = "System.Data.SqlClient" />
    </connectionStrings>
</configuration>

In addition BLToolkit supports an alternative way which uses the <appSettings> section. At the bottom of this page you can find a demo App.config file with a few examples.

BLToolkit recognizes configuration strings in the <appSettings> section by looking for a ConnectionString key prefix. Actual key value can be the following:

key valueProviderConfiguration
key="ConnectionString"default providerdefault configuration
key="ConnectionString.Foo.Bar"'Foo' provider'Bar' configuration
key="ConnectionString.Foo."'Foo' providerdefault configuration
key="ConnectionString.Foo"default provider'Foo' configuration
or'Foo' providerdefault configuration
key="ConnectionString..Foo"default provider'Foo' configuration
key="ConnectionString..Foo.Bar"default provider'Foo.Bar' configuration
Default provider is SqlDataProvider. See also Data Providers.

OpenConfig1.cs
using System;
using System.Data;

using NUnit.Framework;

using BLToolkit.Data;

namespace HowTo.Data
{
    [TestFixture]
    public class OpenConfig1
    {
        [Test]
        public void DefaultConfiguration()
        {
            // Default configuration and default data provider.
            //
            using (DbManager db = new DbManager())
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }

        [Test]
        public void DevelopmentConfiguration()
        {
            // Development configuration and default data provider.
            //
            using (DbManager db = new DbManager("Development"))
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }

        [Test]
        public void ProductionConfiguration()
        {
            // Production configuration and default data provider.
            //
            using (DbManager db = new DbManager("Production"))
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }

        [Test]
        public void OleDbDefaultConfiguration()
        {
            // Default configuration and OleDb data provider.
            //
            using (DbManager db = new DbManager("OleDb"))
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }

        [Test]
        public void OleDbDevelopmentConfiguration()
        {
            // Development configuration and OleDb data provider.
            //
            using (DbManager db = new DbManager("OleDb", "Development"))
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }

        [Test]
        public void OleDbProductionConfiguration()
        {
            // Production configuration and OleDb data provider.
            //
            using (DbManager db = new DbManager("OleDb", "Production"))
            {
                Assert.AreEqual(ConnectionState.Open, db.Connection.State);
            }
        }
    }
}
App.config
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add
            name             = "DemoConnection"
            connectionString = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"
            providerName     = "System.Data.SqlClient" />
    </connectionStrings>
    <appSettings>
        <!-- Default configuration -->
        <add key="ConnectionString"                   value="Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
        <!-- SQL Server Development configuration -->
        <add key="ConnectionString.Development"       value="Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
        <!-- SQL Server Production configuration -->
        <add key="ConnectionString.Production"        value="Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
        <!-- SQL Server configuration -->
        <add key="ConnectionString.Sql"               value="Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
        <!-- OLEDB configuration -->
        <add key="ConnectionString.OleDb"             value="Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=BLToolkitData"/>
        <!-- OLEDB Development configuration -->
        <add key="ConnectionString.OleDb.Development" value="Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=BLToolkitData"/>
        <!-- OLEDB Production configuration -->
        <add key="ConnectionString.OleDb.Production"  value="Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=BLToolkitData"/>
    </appSettings>
</configuration>

The default configuration can be set by a configuration file:

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!-- MySql configuration -->
        <add
            key   = "ConnectionString.MySql"
            value = "..."/>
        <add
            key   = "BLToolkit.DefaultConfiguration"
            value = "MySql"/>
    </appSettings>
</configuration>
Create.sql script
 
© 2010 www.bltoolkit.net
[email protected]