C# app.config 文件中的多个 SQL Server 连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1530284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:28:39  来源:igfitidea点击:

Multiple SQL Server connection strings in app.config file

c#sql-serverconnection-stringapp-config

提问by Benjamin Ortuzar

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.

我有兴趣在 Windows 窗体应用程序中显示 N 个单选按钮的列表,供用户选择目标数据库服务器。我想在 app.config 文件中添加 SQL Server 连接字符串,以便应用程序在运行时读取它们并在 Windows 窗体中呈现为单选按钮。

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?

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

采纳答案by marc_s

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

要从 app.config 中查找所有定义的连接字符串,请使用ConfigurationManager(来自 System.Configuration)。

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

它有一个 enumeration:ConfigurationManager.ConnectionStrings它包含<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 Nameis just the symbolic name you give your connection string - it can be anything, really.

Name只是您为连接字符串提供的符号名称 - 它可以是任何东西,真的。

The ConnectionStringis the connection string itself.

ConnectionString是连接字符串本身。

The ProviderNameis the name of the provider for the connection, e.g. System.Data.SqlClientfor 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).

ProviderName是供应商的连接,例如名称System.Data.SqlClient为SQL Server(和其他人对于其他数据库系统)。如果providerName=在配置中省略连接字符串中的属性,则默认为 SQL Server (System.Data.SqlClient)。

Marc

马克

回答by Frederik Gheysels

Yes, it is possible to do this in another way. Check the connectionStrings section that you can make in the app.config file.

是的,可以通过其他方式做到这一点。检查可以在 app.config 文件中创建的 connectionStrings 部分。

<configuration>
   <connectionStrings>
       <add name="" connectionString=""/>
        <add name="" connectionString=""/>
    </connectionStrings>
</configuration>

回答by d91-jal

Use the connectionStrings section to define your connection strings.

使用 connectionStrings 部分来定义您的连接字符串。

<connectionStrings>
    <add name="connection1" connectionString="user id=user;password=123;server=10.0.0.1;database=myDatabase;connection timeout=30"/>
    <add name="connection2" connectionString="user id=user;password=123;server=10.0.0.2;database=myDatabase;connection timeout=30"/>
</connectionStrings>

回答by GvS

You can use the AppSettingsclass, get a list of all keysthat start with ConnectionString and display them.

您可以使用AppSettings类,获取以 ConnectionString 开头的所有的列表并显示它们。

Your config file will look like this:

您的配置文件将如下所示:

<appSettings>
  <add key="ConnectionString_Name1" value="..."/>
  <add key="ConnectionString_Name2" value="..."/>
  <add key="ConnectionString_Name3" value="..."/>
</appSettings>

You can get the name, by splittingthe key name (using "_" in this example).

您可以通过拆分键名(在本例中使用“_”)来获取名称。

BTW: You shouldalso use the ConnectionStringssection, you are only interrested in connection strings.

顺便说一句:您还应该使用ConnectionStrings部分,您只关心连接字符串。

回答by Deepnath Kundu

We can declare multiple connection string under Web.Config or App.Config

我们可以在 Web.Config 或 App.Config 下声明多个连接字符串

<connectionStrings>
<add name="SourceDB" connectionString="..." />
<add name="DestinationDB" connectionString="..." />
</connectionStrings>

In DAL or .cs file you can access connection strings like this string SounceConnection = ConfigurationManager.ConnectionStrings["SourceDB"].ConnectionString; string DestinationConnection = ConfigurationManager.ConnectionStrings["DestinationDB"].ConnectionString;

在 DAL 或 .cs 文件中,您可以像这样访问连接字符串 string SounceConnection = ConfigurationManager.ConnectionStrings["SourceDB"].ConnectionString; string DestinationConnection = ConfigurationManager.ConnectionStrings["DestinationDB"].ConnectionString;

回答by Sergei Zinovyev

This is how to use LINQ to get list of connection strings:

这是使用 LINQ 获取连接字符串列表的方法:

List<string> connectionStrings = ConfigurationManager.ConnectionStrings
    .Cast<ConnectionStringSettings>()
    .Select(v => v.ConnectionString)
    .ToList();

Or you can build a dictionary of it:

或者你可以建立一个字典:

Dictionary<string/*name*/, string/*connectionString*/> keyValue = ConfigurationManager.ConnectionStrings
    .Cast<ConnectionStringSettings>()
    .ToDictionary(v => v.Name, v => v.ConnectionString);