简体   繁体   中英

How do you connect to a DataBase from a ComboBox?

Connection string file in app.config :

<connectionStrings>
    <add name ="test1" connectionString = "Data Source=
              (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.1)(PORT=1521)))
              (CONNECT_DATA=(SERVICE_NAME=test1)));User Id=test1;Password=test1;" />

    <add name ="test2" connectionString = "Data Source=
              (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.0.1)(PORT=1521)))
              (CONNECT_DATA=(SERVICE_NAME=test1)));User Id=test2;Password=test2;" />
    </connectionStrings>

File in Form loading:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();           
    }

    static string conString = ConfigurationManager.ConnectionStrings["test1"].ConnectionString;
    OracleConnection con = new OracleConnection(conString);
    OracleCommandBuilder cmdbld;
    OracleDataAdapter da;
    DataSet ds;


    private void button1_Click(object sender, EventArgs e)
    {
        //Load Data
        try
        {
            con.Open();
            da = new OracleDataAdapter("sql command", con);
            ds = new DataSet();
            cmdbld = new OracleCommandBuilder(da);
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
}

So, how do I use a ComboBox to change connection between test1 and test2 here?
Right now, I only replace test1 to test2 but putting comboBox1.Text in connectionStrings[] doesn't work. What can I do here?

I can make another log in form and make one form for test1 and the other for test2 connection but it seems very inefficient way to switch between server.

You should move the code, that creates the connection down into the click eventhandler, then you can use comboBox1.Text in the connectionstring.

private void button1_Click(object sender, EventArgs e)
{
   //Load Data
   try
   {
      string conString = ConfigurationManager.ConnectionStrings[comboBox1.Text].ConnectionString;
      OracleConnection con = new OracleConnection(conString);
      con.Open();
      da = new OracleDataAdapter("sql command", con);
      ds = new DataSet();
      cmdbld = new OracleCommandBuilder(da);
      da.Fill(ds);
      dataGridView1.DataSource = ds.Tables[0];
   }
   catch (Exception ex)
   {
        MessageBox.Show(ex.Message);
   }
   finally
   {
       con.Close();
   }
}

If it works when you manually put test1 or test2 in as the index for ConfigurationManager.ConnectionStrings[] then your problem is with what is being returned from your comboBox1.

You don't show us that code, so we can't tell you exactly what is wrong, but try something like this:

var desiredServer = comboBox1.Text;
static string conString = ConfigurationManager.ConnectionStrings[desiredServer].ConnectionString;

and put a breakpoint on the second line to see exactly what you are really putting in there. If this does not solve your problem, then post your combobox and let us try again with better information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM