How to figure out Configuration File used to create the Database object

Topics: Data Access Application Block, Enterprise Library Core, Logging Application Block
Feb 1, 2007 at 1:36 PM
As the database object can be created by using default app.config or user provided configuration file Ex:db1.config, how can i programatically read what configuration source file is the current database object is using.

Example..

CASE 1: Database db = DatabaseFactory.CreateDatabase(); -- Uses App.config

CASE 2: FileConfigurationSource dataSource = new FileConfigurationSource("db1.config"); --- uses db1.config
DatabaseProviderFactory dbFactory = new DatabaseProviderFactory(dataSource);
Database db = dbFactory.Create("connectionstring");

In run-time from one of the database functions is their a way to figure which configuration file the current Database object is using?

Thanks
Feb 1, 2007 at 2:43 PM
No. The Database Class does not care about configuration files. It only cares about connection strings and database provider factories.

Here is another example of creating a database object-

Database db = new SqlDatabase("connection string");

In this case creating a database instance had nothing to do with a configuration file.

Regards,

Dave

_______________________

David Hayden
Microsoft MVP C#
Feb 2, 2007 at 5:24 AM
Connection string is read from config file right?

so their should be a link between database and the datasource, through the database provider factory if i'm not mistaken?

i want to know the configuration file name from where the connection string is read in runtime in the database.cs file.
Feb 2, 2007 at 2:03 PM
The connection string can be read from the config file but it does not have to be read from the config file.

Per the example I gave you above-

Database db = new SqlDatabase("DataSource=...");

I am adding the connection string manually. I am not using a configuration file.

You don't have to use a configuration file to use the DAAB. I haven't tried every block, but you can programmatically use the classes themselves in Enterprise Library and not deal with a configuration source at all.

If you check the documentation and look at the Database class you will not find any properties or methods that tell you where the connection string came from.

Regards,

Dave

______________________

David Hayden
Microsoft MVP C#
Feb 3, 2007 at 7:42 PM
Edited Feb 3, 2007 at 7:44 PM
Hello,

I need a example of configuration file for use with ORACLE ...
I try use Database db = new OracleDatabase("DataSource=..."); but does not work.

Thanks for help

Paulo Moreno
Feb 4, 2007 at 5:35 AM
I don't have Oracle installed anywhere so this is simply off the top of my head, but assuming you have configured your config correctly you should find something along the lines in the connectionStrings section:

<connectionStrings>
<add
name="MyOracleDB"
providerName="System.Data.OracleClient"
connectionString="server=SERVERNAME;user id=USERNAME;password=PASSWORD" />
</connectionStrings>

Now you can do the following to use that connectionString:

IConfigurationSource configurationSource = new SystemConfigurationSource();
DatabaseConfigurationView view = new DatabaseConfigurationView(configurationSource);
ConnectionStringSettings data = view.GetConnectionStringSettings("MyOracleDB");
OracleDatabase oracleDatabase = new OracleDatabase(data.ConnectionString);
DbConnection connection = oracleDatabase.CreateConnection();

and off you go!

Like David mentioned, you can also pass the connectionString as a string value and don't need the configuration file at all. Hope that helps you out.


Regards,
Karl
Feb 4, 2007 at 2:59 PM
Edited Feb 4, 2007 at 3:00 PM

Look my configuration file

*<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<dataConfiguration defaultDatabase="Producao" />
<connectionStrings>
<add name="Producao" connectionString="User Id=paulo;Password=moreno;Server=PRODU-SERVER;"
providerName="System.Data.OracleClient" />
</connectionStrings>
</configuration>*

but when I create database return this error message

System.Configuration.ConfigurationException was unhandled
Message="Invalid section name. The section 'dataConfiguration' does not exist in the requested configuration file 'C:\\Fontes\\Produção\\DotNet\\Dix Dental\\EnvioLiberacoesEmail\\bin\\DixEnviaLibEmail.vshost.exe.config' or the file 'C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\Config\\machine.config'. Make sure that the 'enterpriseLibrary.configurationSections' configuration section exists in one of the files and that the section 'dataConfiguration' is defined.

thanks again
Feb 4, 2007 at 6:36 PM
The configuration file appears to look good.

I think the problem is that the configuration information is not in the right file / location.

Enterprise library is looking for it here according to your error message-

DixEnviaLibEmail.vshost.exe.config ( which typically gets generated from the app.config file )

and it can't find the configuration information.

Check the file and see if it is there.

Regards,

Dave

______________________

David Hayden
Microsoft MVP C#
Feb 5, 2007 at 10:35 PM
Thanks,

But the problem was in version of framework, I use VS 2005 and VS 2003 in the same computer.
When I set property specific version = true automatically runtime change to v2.050727 and no more errors shown.

Regards

Paulo Moreno
Feb 14, 2007 at 8:02 PM
Edited Feb 14, 2007 at 8:28 PM
I have a similar problem. Here is my App.config file in the project root:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<dataConfiguration defaultDatabase="LocalDev" />
<connectionStrings>
<add name="LocalDev" connectionString="Database=MyDatabase;Server=.\SQL2005;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>


Here is the code block:

public static class SqlHelper
{
public const string DEFAULT_CONNECTION = "LocalDev";

public static int ExecuteNonQuery(string query)
{
int rowsAffectedCount = 0;

Database db = DatabaseFactory.CreateDatabase(DEFAULT_CONNECTION);
DbCommand comm = db.GetSqlStringCommand(query);
rowsAffectedCount = db.ExecuteNonQuery(comm);

return rowsAffectedCount;
}
}

The above lives in an assembly called AS.Com.Data.

I reference this DLL into a console application and call the static method like this:
Console.Write(SqlHelper.ExecuteNonQuery("some select statement"));

And I get this exception as a result....

System.Configuration.ConfigurationErrorsException was unhandled
Message="The requested database LocalDev is not defined in configuration."
Source="Microsoft.Practices.EnterpriseLibrary.Data"
BareMessage="The requested database LocalDev is not defined in configuration."
Line=0
StackTrace:
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseConfigurationView.ValidateConnectionStringSettings(String name, ConnectionStringSettings connectionStringSettings)
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseConfigurationView.GetConnectionStringSettings(String name)
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUpTTypeToBuild(IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUpT(IReadWriteLocator locator, String id, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUpT(String id, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.Create(String name)
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase(String name)
at AS.Com.Data.SqlHelper.ExecuteNonQuery(String query) in D:\Data\Projects\Club\Source\Trunk\GlobalComponents\Com.Data\SqlHelper.cs:line 16
at AS.Com.Business.Project.ProjectManager.AddProject(ProjectBase newProject) in D:\Data\Projects\Club\Source\Trunk\GlobalComponents\Com.Business\Project\ProjectManager.cs:line 40
at TesterConsole.Program.Main(String[] args) in D:\Data\Projects\Club\Source\Trunk\Prototype\Club.Prototype\TesterConsole\Program.cs:line 23
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


I am very confused. Why am I getting this exception?

Thanks
Feb 15, 2007 at 2:34 PM
According to your description, you shouldn't be getting the error.

If the App.config with the database configuration is indeed in your console application project, you shouldn't get the error.

Although the above should work, I would remove this line from your Data Assembly:

public const string DEFAULT_CONNECTION = "LocalDev";

and change the code:

Database db = DatabaseFactory.CreateDatabase(DEFAULT_CONNECTION);

to

Database db = DatabaseFactory.CreateDatabase();

You have already set the default database as LocalDev in your App.config file, so you don't need to do it programmatically. This also allows you to change the default database in the App.config file alone without changing any code.

The only reason I can see ( without having the actual code ) why the code above may fail is if you don't have the database configuration information in the console application's App.config. By default, the DAAB will use SystemConfigurationSource ( your application's web.config or app.config ) to get the configuration information unless you specify otherwise.

Regards,

Dave

_______________________

David Hayden
Microsoft MVP C#