vb.net ConfigurationManager.ConnectionStrings["XXX"] 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16565275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:36:03  来源:igfitidea点击:

ConfigurationManager.ConnectionStrings["XXX"] is returning null

c#asp.netvb.net

提问by Lydia

I have a console application in which website project is added as dll. Inside dll ConfigurationManager.ConnectionStrings["XXX"]is called which is always returning null.

我有一个控制台应用程序,其中将网站项目添加为 dll。内部 dllConfigurationManager.ConnectionStrings["XXX"]被调用,它总是返回null.

But web.config in dll has connectionstring named 'XXX'. Can any one suggest what is going wrong?

但是 dll 中的 web.config 具有名为“XXX”的连接字符串。任何人都可以建议出了什么问题吗?

回答by David S.

I don't think .config file of the .dll is added to your console project together with the dll. You need to have the .config in your executing project.

我认为 .dll 的 .config 文件不会与 dll 一起添加到您的控制台项目中。您需要在执行项目中拥有 .config。

回答by Jerry

Try copying the config file to your console project. Or at least the connection strings section. Probably in an App.config.

尝试将配置文件复制到您的控制台项目。或者至少是连接字符串部分。可能在 App.config 中。

回答by Serge

web.config is for web application.

web.config 用于 Web 应用程序。

For console application you have to use app.config.

对于控制台应用程序,您必须使用 app.config。

Well, actually that's the default configuration for C#, I won't be surprised some advanced user could make it go the other way arround.

嗯,实际上这是 C# 的默认配置,我不会感到惊讶一些高级用户可以让它反过来。

回答by Peter Holloway

I had a slightly different problem that I haven't seen an answer to. I was using.

我有一个稍微不同的问题,我还没有看到答案。我正在使用。

ConfigurationManager.ConnectionStrings["XXXX"].ConnectionString;

ConfigurationManager.ConnectionStrings["XXXX"].ConnectionString;

When I opened up web.config I saw that the connection string I had created was prefixed with "WebApplicationAPI.Properties.Settings." - putting the entire string in worked for me:

当我打开 web.config 时,我看到我创建的连接字符串以“WebApplicationAPI.Properties.Settings”为前缀。- 将整个字符串放入对我有用:

ConfigurationManager.ConnectionStrings["WebApplicationAPI.Properties.Settings.XXXX"].ConnectionString;

ConfigurationManager.ConnectionStrings["WebApplicationAPI.Properties.Settings.XXXX"].ConnectionString;

回答by Dave

Your config file is not in the .dll. So you're referencing nothing I would suspect! Unless you've manually copied the web.config file to the correct location or references it via an absolute path?

您的配置文件不在 .dll 中。所以你没有引用我怀疑的任何东西!除非您手动将 web.config 文件复制到正确的位置或通过绝对路径引用它?