English 中文(简体)
如何读取设置文件创建的连接字符串?
原标题:How do read connection string created by the settings file?

当我刚刚有一个 app. config 时, 每次我尝试配置管理器. appSettings [0] 或配置管理器. appSettings [“键名”] 我都有空。 所以我尝试使用一个. sections 文件, 该文件创建了一个 ap. config, 看起来像这个 。

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="IndexLoader.IndexLoader" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <IndexLoader.IndexLoader>
      <setting name="ConnectionString" serializeAs="String">
        <value>INITIAL CATALOG=xxx;DATA SOURCE=xxx;User ID=xxx;Password=xxx;Application Name=xxx;</value>
      </setting>
    </IndexLoader.IndexLoader>
  </applicationSettings>
</configuration>

我该如何使用 C # 读取连接字符串?

解决方案有多个工程, 其中许多有配置文件。 是否有方法可以指定我要使用代码工程中的配置文件?

最佳回答

applicationSettings is not appSettings. To read an entry in applicationSettings you use

string myCnStr = Properties.Settings.Default.ConnectionString;

狦穦

string myCnStr =IndexLoader.IndexLoader.Default.ConnectionString;

However the ConnectionString has a dedicated section in the app.config and should be stored there and read from the ConfigurationManager.ConnectionStrings collection

http://msdn.microsoft.com/en-us/library/ms254494.aspx" rel=“nofollow'>见此 查询

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签