[英]how to connect to a database on select of any value from combobox
我有一个 combobox 填充了各种数据库名称。 我想从 combobox 连接到任何数据库名称的 select 上的某个数据库。 Gow 我应该和这个有关吗? 代码如下..
private void Form1_Load(object sender, EventArgs e)
{ XmlDocument doc = new XmlDocument();
doc.Load("C:\\Documents and Settings\\user\\Desktop\\abc.xml"); XmlNodeList List = doc.SelectNodes("config/dataSources/dataSource");
foreach (XmlNode dataSources in List)
{ comboBox1.Items.Add(dataSources.Attributes["name"].Value.ToString()); comboBox2.Items.Add(dataSources.Attributes["name"].Value.ToString());
}
}
我有另一个包含连接字符串信息的代码
public class DBConnect
{
string dataSource;
string userId;
string password;
string filepath;
public DBConnect()
{ }
public string ConnectionString()
{ filepath = ReadRegistry("ConfigFile");
XmlDocument doc = new XmlDocument();
doc.Load(@filepath);
XmlNodeList nodes = doc.SelectNodes
("/config/dataSources/dataSource");
foreach (XmlNode node in nodes)
{ if (userId.select == node.Attributes["dataSource"].Value)
{ dataSource = node.Attributes
["dataSource"].Value;
userId = node.Attributes["userId"].Value;
password = node.Attributes["password"].Value;
password = Abc.Security.Encryption.Decode(password);
break;
}
}
string conn = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;Password=" + password + ";User ID=" + userId + ";Data Source=" + dataSource + ";"; return conn; } protected string ReadRegistry(string filename) { Microsoft.Win32.RegistryKey theKey = Microsoft.Win32.Registry.LocalMachine; theKey = theKey.OpenSubKey(@"SOFTWARE\Abc, Inc\Abc Marketing"); if (theKey != null) { //string filePath = theKey.GetValue("ConfigFile").ToString(); filepath = theKey.GetValue(filename).ToString(); theKey.Close();
}
return filepath;
}
那么现在我应该如何 go 关于编写代码,该代码在 select 上来自 combobox 的任何数据库名称的 Z99938283A3C28BDZ 上将我连接到该特定数据库。 我是 c# 的新手,请给我一个解决方案。 我应该在哪里包含代码?
你可以做
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder("server=(local);user id=ab; password= a!Pass113;initial catalog=AdventureWorks");
builder.InitialCatalog = (string)comboBox1.SelectedItem;
对于 Oracle 做
OracleConnectionStringBuilder builder =
new OracleConnectionStringBuilder("");//put connection string here
builder.UserID = (string)comboBox1.SelectedItem;
OracleConnection conn = new OracleConnection(builder.ConnectionString);
OracleCommand cmd = new OracleCommand("Select 1 from dual", conn);
conn.Open();
int temp = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
希望这有帮助
好吧,您可以连接您的 ComboBox 的SelectedIndexChanged 事件,当它触发时,您可以从 ComboBox 的SelectedItem 属性中检索选定的数据库,并在连接字符串中使用它来连接到您的数据库。
例子
您说您已经有一个有效的连接字符串。 您需要做的就是允许您的用户更改该字符串的 DataSource 部分。 您可以使用OracleConnectionStringBuilder.DataSource属性来执行此操作。
在 ComboBox 的 SelectedIndexChanged 事件中更新此属性:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
// A connection string for testing.
string connectString = "Server=OracleDemo;Integrated Security=True"
var builder = new OracleConnectionStringBuilder(connectString);
// Show your connection string before any change.
Console.WriteLine("ConnString before: {0}", builder.ConnectionString);
builder.DataSource = comboBox1.SelectedItem.ToString();
// This will show your conn string has been updated with the user selected database name.
Console.WriteLine("ConnString after: {0}", builder.ConnectionString);
// At this point you're ready to use the updated connection string.
}
您需要让 ComboBox 使用 DropDownStyle.DropDownList 值,这样用户就不能输入自己的数据库名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.