[英]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
更改test1
和test2
之间的连接?
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. 如果在您手动将
test1
或test2
用作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.