[英]How can I access the connection string in my Web.Config file through a Windows Application
I have my Entity Connection String set in my Web.Config file. 我在Web.Config文件中设置了实体连接字符串。 In the same solution, I have a Windows Application, and I want to access the Connection String of my Entity. 在同一解决方案中,我有一个Windows应用程序,并且我想访问我的实体的连接字符串。 How can I access the connection string that exist in my Web.Config file? 如何访问Web.Config文件中存在的连接字符串? Or I have to create a specific one in my Windows Application ? 还是我必须在Windows应用程序中创建一个特定的文件夹?
If you're unfamiliar with adding a config file to a windows app, you must select an "Application Configuration File" from add new items, it will create a file named App.Config which will at compile time become YourProgramsName.exe.config but internally it works mostly just like a web.config with an appsettings section and connectionstrings sections and all the other normal sections. 如果您不熟悉向Windows应用程序添加配置文件的情况,则必须从添加新项中选择“应用程序配置文件”,它将创建一个名为App.Config的文件,该文件在编译时将成为YourProgramsName.exe.config,但在内部,它的工作原理与web.config差不多,其中包含appsettings部分和connectionstrings部分以及所有其他普通部分。
The best way to share this data between Web and Win config files would be: 在Web和Win配置文件之间共享此数据的最佳方法是:
Create a separate file that looks like: 创建一个单独的文件,如下所示:
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
and reference it from both config files with: 并通过以下两个配置文件引用它:
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
Then just add the connections.config (add existing item by link ) to each project. 然后,只需将connections.config( 通过link添加现有项)添加到每个项目。
More reading: http://msdn.microsoft.com/en-us/library/ms254494.aspx 更多阅读: http : //msdn.microsoft.com/en-us/library/ms254494.aspx
将连接字符串放在Windows应用程序的App.config
。
使用此代码:
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.