在类库项目中更改数据库连接串[英] change database connectionstring in class library project

本文是小编为大家收集整理的关于在类库项目中更改数据库连接串的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在解决方案中使用了多个项目,它们是真实的项目,其他项目是帮助项目.我有一个辅助项目之一,我有用于存储数据库连接字符串的设置文件.现在,当我构建/调试时.我看不到任何可以更改连接字符串的文件.它是否已集成到Helproproject的DLL文件中?我该如何解决我可以更改连接字符串的操作系统?

推荐答案

设计器生成的Settings类具有"internal"访问说明符. 因此,您添加的应用程序设置将在参考助手组件的组件中看到.

要使它在参考组件中可见,请手动或通过设计师进行类public.请参阅快照. 在此处输入图像说明

完成public后,您可以在引用此组件的其他项目中访问它.

String connectionString = TestAssembly.Settings.Default.ConnectionString;

,但是最好将设置放入该应用程序特定的应用程序配置文件中.在您的情况下,将app.config添加到主项目中;并在配置中添加ConnectionsRings部分.

其他推荐答案

我想你想念一些东西.您应该在需要app.config文件(或Web.config的情况下,在Web App/site)的任何项目中添加. 然后,通过ConfigurationManager类,您应该访问其中的数据. http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

运行/编译项目后,App.Config将自动复制,并且将其名称为YourApp.exe.config或Yourelib.dll.dll.config.

有关更多详细信息,请在此处阅读/saveconnstringinappconfig.aspx

希望这会有所帮助

本文地址:https://www.itbaoku.cn/post/1557002.html

问题描述

I am using more than one projects in a solution, on e of them are real projects, others are helper projects. One of those helper projects, I have settings.settings file for storing database connection string. Now When I build/debug it. I don't see any file where I can change the connection string. Was it gone integrated in the helperproject's dll file? How can i resolve this os that i can change the connection string please?

推荐答案

The Settings class generated by the designer has "internal" access specifier by default. So the application settings you've added will not be visible in the assembly that is referring the helper assembly.

To make it visible across the referring assemblies, make the class public manually or via the designer. See the snapshot. enter image description here

After you've made it public, you can access it in other projects where this assembly is referred.

String connectionString = TestAssembly.Settings.Default.ConnectionString;

But it would be better to put the settings in a application configuration file specific to that application. In your case add an app.config to the main project; and add connectionStrings section in the config.

其他推荐答案

I think that you miss understood something. You should add at any project you want the app.config file (or web.config in case of web app / site). Then via ConfigurationManager class you should access the data in it. http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

The app.config is automatically copied once you run/compile your project and it will have a name as youreapp.exe.config or yourelib.dll.config.

For more details read here http://www.codeproject.com/KB/files/SaveConnStringInAppConfig.aspx

Hope this helps