在app.config文件多个SQL Server连接字符串 [英] Multiple SQL Server connection strings in app.config file

查看:171
本文介绍了在app.config文件多个SQL Server连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很感兴趣,在Windows窗体应用程式显示N个单选按钮列表供用户选择目标数据库服务器。我想补充的SQL Server连接字符串中的app.config文件,因此它们在运行时由应用程序读取和窗口形式单选按钮呈现。

I'm interested in displaying in a Windows Forms app a list of N radio buttons for the user to choose a target database server. I would like to add the SQL Server connection strings in the app.config file, so they are read by the app at runtime and rendered in the windows form as radio buttons.

起初我以为用分隔符分开连接

At first I thought of using a delimiter to separate the connections

  <appSettings>
    <add key="ConnectionString" value="connection1|user id=user;password=123;server=10.0.0.1;database=myDatabase;connection timeout=30|connection2|user id=user;password=123;server=10.0.0.2;database=myDatabase;connection timeout=30"/>
</appSettings>



再拆键值对。

And then split the key value pairs.

是否有可能做到这一点以不同的方式?

Is it possible to do this in a different way?

推荐答案

要找到你的app.config所有定义的连接字符串,使用 ConfigurationManager中(从System.Configuration)

To find all defined connection strings from your app.config, use the ConfigurationManager (from System.Configuration).

它有一个枚举: ConfigurationManager中.ConnectionStrings 包含在<的所有项目;是connectionStrings方式>

It has an enumeration: ConfigurationManager.ConnectionStrings which contains all entries in your <connectionStrings>.

您可以遍历其与此代码:

You can loop over it with this code:

foreach(ConnectionStringSettings css in ConfigurationManager.ConnectionStrings)
{
   string name = css.Name;
   string connString = css.ConnectionString;
   string provider = css.ProviderName;
}



名称是只是象征性的名称,你给你的连接字符串 - 它可以是任何东西,真的

The Name is just the symbolic name you give your connection string - it can be anything, really.

的ConnectionString 是连接字符串本身。

的ProviderName 是供应商的连接,如:名称 System.Data.SqlClient的为SQL Server(和其他人用于其他数据库系统)。如果您在配置您的连接字符串,它默认到SQL Server(System.Data.SqlClient的)。

The ProviderName is the name of the provider for the connection, e.g. System.Data.SqlClient for SQL Server (and others for other database system). If you omit the providerName= attribute from your connection string in config, it defaults to SQL Server (System.Data.SqlClient).

马克

这篇关于在app.config文件多个SQL Server连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆