简体   繁体   中英

Where to store connection string for class library in desktop application? Can I use in app.config?

I am new to desktop application development and currently building a desktop application using layered architecture (user interface, DAL, BLL).

In web development I used to store the connection string in web.config and my class library was accessing it from there. Please guide me how & where connection string should be stored for DAL in desktop application. I tried to add an app.config file in my class library and access the connection string like this:

ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString)

But it throuws an error: "Object reference not set to an instance of an object."

Kindly guide me on this. Thanks for your support and sharing.

It should be stored in the app.config of the Windows Application and not the class library. Basically when you run your executable there should be a file called Foo.exe.config (where Foo.exe is the resulting executable of your Windows Application) in the same folder which contains the settings.

So in Visual Studio simply add an app.config file to the WinForms application project and store the settings there. In this case they will be successfully read by your custom library. There's no need to add an app.config file to your class library project as it will never be used.

Yes, in a desktop application, all configuration should be in the app.config for that application. The class libraries used by this desktop application will get their config from that app.config by default.

If this line throws an exception:

ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString

then it's most likely because no <connectionStrings> entry of name "connectionstring" exists. Check for NULL:

if(ConfigurationManager.ConnectionStrings["connectionstring"] != null)
{
    string connStr = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
 }

It will be stored in app.config file and you can access the connection string as you do in web application:

if(ConfigurationManager.ConnectionStrings["connectionstringName"] != null)
{  
      string connectionString = ConfigurationManager.ConnectionStrings["connectionstringName"].ConnectionString;
}

You can use app.config or other way you can use simple text file mentioning with connection string in it.

//// Read connection string from the text file
StreamReader sr = new StreamReader(System.Windows.Forms.Application.StartupPath @"C:\ConnectionString.txt");
connStr = sr.ReadLine();

Any configuration settings for an application whether app settings or connection strings should be placed in the application's app.config file. In case of your desktop application you can add an "Application Configuration" file or "app.config" and place your connection string in there. Any dependency for that application .. eg a class library like a DAL will pull the value it needs for its connection string from the application's *.config file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM