【问题标题】:How to set connection string by using combobox selected item?如何使用组合框选定项设置连接字符串?
【发布时间】:2014-02-08 22:29:32
【问题描述】:

我有两个公共连接字符串。我认为这种方法没有用。我希望用户在单击按钮连接到数据库之前从组合框中进行选择。

public partial class Form1 : Form
{
    public string CompanyA = "Data Source=.;Initial Catalog=compA;User ID=sa;Password=**";
    public string CompanyB = "Data Source=.;Initial Catalog=compB;User ID=sa;Password=***";

    public Form1()
    {
        InitializeComponent();
    }
}

我在代码中使用了 CompanyA 和 CompanyB 字符串。

using (SqlConnection con = new SqlConnection(CompanyA ))
{
    // do somethings....
}

using (SqlConnection con = new SqlConnection(CompanyB))
{
    // do somethings....
}

如何通过这样的组合框选择连接字符串? (对不起,错误的架构)

private void ConnectButton_Click(object sender, EventArgs e) 
{
    if ( (comboBox1.SelectedItem=="compA") && ( (comboBox2.SelectedItem=="compB") )
    {
        // public string CompanyA = "Data Source=.;Initial Catalog=compA;User ID=sa;Password=**";
        // public string CompanyB = "Data Source=.;Initial Catalog=compB;User ID=sa;Password=**";
    }
}

【问题讨论】:

  • 您能澄清一下您要做什么吗?组合框和连接字符串之间的关系是什么?
  • 先生,我不想对所有代码使用公共字符串。我希望用户从组合框中进行选择。用户必须知道连接到哪个公司。

标签: c#


【解决方案1】:

您可以通过覆盖类上的“ToString”方法将任何您想要的内容放入组合框中。一个简单的例子:

class ComboItemExample {
   public string DisplayString { get; set; }
   public string ConnectionString { get; set; }

   public override string ToString() { return DisplayString; }
}

一旦你定义了这个,你就可以实例化一些并将它们添加到组合框中。该代码如下所示:

private string currentConnection = "<default connection>";

public Form1() {
   InitializeComponent();

   var firstConnection = new ComboItemExample { DisplayString = "Local Database", ConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" };
   myComboBox.Items.Add(firstConnection);

   var secondConnection = new ComboItemExample { DisplayString = "Other Database", ConnectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" };
   myComboBox.Items.Add(secondConnection);
}

最后,您希望在用户更改所选项目时使用这些对象。该代码类似于:

public void myComboBox_SelectedIndexChanged(object sender, EventArgs e) {
  if (myComboBox.SelectedIndex <= 0) return;
  var newConnection = ((ComboItemExample)myComboBox.Items[myComboBox.SelectedIndex]).ConnectionString;

  // now use "newConnection" as your connection string. Save in a local member
  //    or pass directly into the call to create the database connection.
  currentConnection = newConnection;
}

应该这样做!我希望这会有所帮助!

澄清/修改

为了澄清,您可以稍后使用此代码创建连接:

using (var connection = new SqlConnection(currentConnection)) {
   // use the connection here
}

我还在上面的代码中添加了一个模块变量,以明确在哪里声明和设置当前连接字符串。祝你好运!

【讨论】:

  • @user3107343 要么声明一个模块变量“currentConnection”,然后在创建连接时使用它,要么总是使用“SqlConnection(((ComboItemExample)myComboBox.Items[myComboBox.SelectedIndex]).ConnectionString)”
  • @user3107343 在我的回答中添加了说明。希望有帮助!
  • 先生为什么我得到这个错误 ConnectionString 属性没有被初始化。在 Line connection.Open();
【解决方案2】:

你可以做一个连接字符串检查

if (this.comboBox1.SelectedText == "compA")
{
    // connect using the companyA connectionstring
}
else
{
    // connect using the companyB connectionstring
}

【讨论】:

  • 如何使用公共字符串检查组合框?你觉得有可能吗?
  • 我真的不明白公共字符串检查是什么意思?我了解到您想查看何时使用哪个公司的哪个连接字符串
猜你喜欢
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2010-10-01
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多