views:

342

answers:

4

Hi All,

I'm looking for a way to configure a DB connection at runtime; specifically using the Enterprise Library. I see that there's a *.Data.Configuration (or something close to this ... don't recall off the top of my head) assembly but am finding not much on the interwebs. Complicating matters is the fact that the API help is broken on Vista.

Now, I found this work-around:

Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringSettings connection = new ConnectionStringSettings();
connection.Name = "Runtime Connection";
connection.ProviderName = "System.Data.OleDb";
connection.ConnectionString = "myconstring";
cfg.ConnectionStrings.ConnectionStrings.Add(connection);
cfg.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("connectionStrings");
var runtimeCon = DatabaseFactory.CreateDatabase("Runtime Connection");

And although it gives me what I want, it permanently edits the App.config. Sure I can go back and delete the changes, but I'd rather not go through this hassle.

Thanks!

+1  A: 

If you're using a winforms app you could try using UserProperties to store this info. Another possible solution could be custom configuration sections.

lomaxx
A: 

If you don't want it saved, you do not need to execute the cfg.Save command.

The Configuration object will store your changes until it isn't needed anymore.

Jon Limjap
A: 

I hadn't tried just seeing if EntLib would see the changes w/o the Save() (the site I got it from seemed to suggest Save() was necessary). I'll give it a try.

@lomaxx No, this is not a SWF app. Funny, lomaxx, I just added to your comment in the "Hidden Features of C#" thread. Small interworld.

xanadont
A: 

Nope, you must save in order for the EntLib (and, I suspect, any other tool) to see the changes.

xanadont