简体   繁体   English

如何通过Windows应用程序访问Web.Config文件中的连接字符串

[英]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.

相关问题 在C#中,如何读取存储在web.config文件连接字符串中的连接字符串? - In C# , how can I read a connection string stored in my web.config file connection string? 如何在C#中访问web.config连接字符串? - How to access web.config connection string in C#? 如何从web.config添加和访问连接字符串 - How to add and access connection string from web.config 在 ASP.NET MVC 的 web.config 文件中找不到我的连接字符串标记 - Can't find my connection string tag in web.config file in ASP.NET MVC 如何在 web.config 文件中写入连接字符串并从中读取? - How to write connection string in web.config file and read from it? 如何从web.config文件中获取连接字符串? - How to fetch connection string from web.config file? 如何将连接字符串和IP地址放入web.config文件中? - How do I put connection string with IP Address inside web.config file? 如何在web.config之外的ASP.Net中保留连接字符串? - How can I persist a connection string in ASP.Net outside of web.config? 如何在Web.Config中的2个连接字符串之间切换(为DBML激活一个) - How can I switch between 2 Connection Strings in my Web.Config (Activate one for DBML) 如何从C#中另一个应用程序的app.config / web.config文件中提取连接字符串 - How to extract connection string from another application's app.config / web.config file in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM