简体   繁体   English

如何从ComboBox连接到数据库?

[英]How do you connect to a DataBase from a ComboBox?

Connection string file in app.config : 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: Form加载文件:

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? 那么,如何在此处使用ComboBox更改test1test2之间的连接?
Right now, I only replace test1 to test2 but putting comboBox1.Text in connectionStrings[] doesn't work. 现在,我仅将test1替换为test2但是将comboBox1.Text放入connectionStrings[]中不起作用。 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. 我可以建立另一个登录表单,为test1建立一个表单,为test2建立另一个表单,但是在服务器之间切换似乎很低效。

You should move the code, that creates the connection down into the click eventhandler, then you can use comboBox1.Text in the connectionstring. 您应该将创建连接的代码向下移动到click事件处理程序中,然后可以在连接字符串中使用comboBox1.Text。

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. 如果在您手动将test1test2用作ConfigurationManager.ConnectionStrings[]的索引时有效,则问题出在从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. 如果这样做不能解决您的问题,请发布您的组合框,让我们用更好的信息再试一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将组合框绑定到数据库并显示枚举中的值,或者这是正确的方法? - How do you bind a combobox to a Database and show values from an enum, or is this the right approach? 如何填充数据库中的集合并将该集合连接到ComboBox? - How I can fill a collection from a database and connect this collection to ComboBox? 如何从 combobox 连接到 select 上的任何值的数据库 - how to connect to a database on select of any value from combobox 您如何进行类连接以连接到远程数据库? - how do you do a class connection to connect to a remote database? 如何从数据库中将数据加载到组合框中 - How do I load data into combobox from database 如何从数据库加载ComboBox列表? - How do I load the list of a ComboBox from the database? 如何将数据库中的数据同步到组合框 - How do I sync data from a database to a combobox 如何将绑定连接到(Winform)ComboBox 控件以从绑定源获取/设置控件中的选择? - How do I connect a binding to a (Winform) ComboBox control to get/set the selection in the control from the binding source? 如何将ComboBox连接到WPF的C#中的数据库文件 - How to connect ComboBox to database file in C# for WPF 如何从C#连接到SQL数据库? - How do I connect to a SQL database from C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM