首先你需要获取Xml子节点的值,在以前的文章中提到过,
XmlDocument doc = new XmlDocument();
string url = Application.StartupPath.ToString();
doc.Load(@url + "\\xml_config.xml");
XmlNode xnRoot = doc.SelectSingleNode("sources");
XmlNode xml_cbhs = xnRoot.SelectSingleNode("source_c");
uid.Text = xml_cbhs.SelectSingleNode("c_uid").InnerText;
pwd.Text = xml_cbhs.SelectSingleNode("c_pwd").InnerText;
ip.Text = xml_cbhs.SelectSingleNode("c_ip").InnerText;
port.Text = xml_cbhs.SelectSingleNode("c_port").InnerText;
然后对获取到的值来完成数据库连接字符串的拼接
string orlStr = @"Provider='OraOleDb.Oracle';User ID=" + uid.Text.ToString().Trim() + ";Password=" + pwd.Text.ToString().Trim() + ";Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = " + ip.Text.ToString().Trim() + ")(PORT = " + port.Text.ToString().Trim() + "))) (CONNECT_DATA = (SERVICE_NAME = ORCL)))";
最后附上数据库连接测试需要用到的:
OleDbConnection conn = new OleDbConnection(orlStr);
try
{
conn.Open();//打开指定的连接
MessageBox.Show("连接成功");
//button7.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
//开发过程中很重要的就是要把打开测试的数据库关闭。以免引发不必要的麻烦
}