0

由于我正在开发的应用程序需要访问多个服务器,因此我希望允许用户从下拉列表中选择他们希望访问的“站点”,并相应地更新连接字符串。

我有两个想法(伪代码);

IF (drop-down value = Site2)
{string connectionString = "Server=server2; Database=db......"}

ELSE IF(drop-down value = Site3.....

或者;

string @server = drop-down value;

string connectionString = "Server=@server; Database=db......"
"pass connection variable??"

这些方法中哪个最好?还是有更有效的选择?

我不确定如何将变量传递给连接字符串(选项 2)。

4

4 回答 4

3

看看SqlConnectionStringBuilder 类

例如

var builder = new SqlConnectionStringBuilder();
builder.DataSource = /* your server here */;
builder.InitialCatalog = "db";
/* etc.... */
var connString = builder.ConnectionString;
于 2013-03-29T14:26:07.270 回答
2
    string ConnectionString;
    switch (comboBox1.SelectedIndex)
    {
        case 0:
            ConnectionString = "Data Source=server1;Initial Catalog=database1;User ID=user1;Password=password1";
            break;
        case 1:
            ConnectionString = "Data Source=server2;Initial Catalog=database2;User ID=user1;Password=password2";
            break;
        case 3:
            ConnectionString = "Data Source=server3;Initial Catalog=database3;User ID=user1;Password=password3";
            break;
    }
    SqlConnection Con = new SqlConnection(ConnectionString);
    Con.Open();
于 2013-03-29T14:27:45.387 回答
1

如果您需要“超级动态”,那么 Davio 有正确的选择。

如果您需要“我有 N 个数据库,可能会不时更改”,那么我建议:

以下是典型的 Dot Net 2.0 及以上配置。

<connectionStrings>
<add name="EastCoastConnectionString" connectionString="server=server01;database=myDb;uid=myUser;password=myPass;" />
<add name="WestCoastConnectionString" connectionString="server=server02;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

然后创建一个自定义配置部分,将“友好名称”映射到连接字符串。

查找此代码: 如何在 ConfigurationSection 中包含简单集合

  <TransformationToDirectoryMappingsSection>
    <TransformationToDirectoryMappings>
      <add FriendlyName="NY Giants" PickupFolder="EastCoastConnectionString" />
      <add FriendlyName="NE Patriots" PickupFolder="EastCoastConnectionString" />
      <add FriendlyName="Carolina Panthers" PickupFolder="EastCoastConnectionString" />

      <add FriendlyName="San Francisco 49ers" PickupFolder="WestCoastConnectionString" / >
      <add FriendlyName="Seattle Seahawks" PickupFolder="WestCoastConnectionString" />
      <add FriendlyName="Arizona Cardinals" PickupFolder="WestCoastConnectionString" />
    </TransformationToDirectoryMappings>
  </TransformationToDirectoryMappingsSection>
</configuration >

然后用所有“FriendlyName”填充一个下拉框......然后您可以将其映射回连接字符串。

现在,您可以使用连接字符串的名称填充下拉列表......但我不喜欢这个想法......我更喜欢“友好名称”到“实际名称”的映射。


至少,在您的配置文件中设置连接字符串.....,并使用以下内容填充您的下拉列表:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx

  ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;

    foreach (ConnectionStringSettings connection in connections)
    {
      string name = connection.Name;

    }
于 2013-03-29T14:36:43.147 回答
0

除了使用 SqlConnectionStringBuilder 之外,我还将显示名称和连接字符串包装在表单中的嵌套类中。像这样(在表单/用户控件中):

    private void PopulateComboBox(ComboBox comboBox)
    {
        var items = new[]
            {
                new ConnectionListItem {DisplayName = "Site 1", ConnectionString = new SqlConnectionStringBuilder("Server=...")},
                new ConnectionListItem {DisplayName = "Site 2", ConnectionString = new SqlConnectionStringBuilder("Server=...")},
            };

        comboBox.Items.AddRange(items);

    }

    class ConnectionListItem
    {
        public string DisplayName { get; set; }
        public SqlConnectionStringBuilder ConnectionString { get; set; }

        public override string ToString()
        {
            return DisplayName;
        }
    }
于 2013-03-29T14:36:58.180 回答