在哪里存储连接字符串中的桌面应用程序的类库?我可以在app.config中使用?字符串、应用程序、类库、桌面

2023-09-05 02:01:07 作者:经年里的落花炙伤

我是新来的桌面应用程序的开发和目前正在建设使用分层架构(用户界面,DAL,BLL)的桌面应用程序。

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

在web开发我用来存放在web.config中的连接字符串和我的类库是从那里访​​问它。请指导我如何和放大器;其中,连接字符串应存放于DAL在桌面应用程序。我想在我的课库中添加的app.config 文件和访问这样的连接字符串:

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)

但throuws错误:。未将对象引用设置到对象的实例的

请指导我在此。感谢您的支持和分享。

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

推荐答案

是的,在一个桌面应用程序,所有的配置应该在的app.config 该应用程序。使用该桌面应用程序将得到他们的配置从的app.config 默认的类库。

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

那么它极有可能是因为没有<的ConnectionStrings> 的名称输入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;
 }