using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; using GeoScene.Globe; using GeoScene.Engine; using System.IO; using System.Xml; namespace Cyberpipe { public partial class FrmFlagDatabase : Office2007Form { public static string dbServer = ""; public static string database = ""; public static string userId = ""; public static string password = ""; public static GSODataSource ds = null; private GSOGlobeControl ctl; public FrmFlagDatabase(GSOGlobeControl _ctl) { InitializeComponent(); ctl = _ctl; } private void btn_ok_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(txt_ip.Text.Trim()) || String.IsNullOrEmpty(txt_db.Text.Trim()) || String.IsNullOrEmpty(txt_user.Text.Trim()) || String.IsNullOrEmpty(txt_pwd.Text.Trim())) { MessageBox.Show("配置参数不能为空!", "提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); return; } try { //ds = ctl.Globe.DataManager.OpenOracleDataSource(txt_ip.Text.Trim() + "/" + txt_db.Text.Trim(), "", "", txt_user.Text.Trim(), txt_pwd.Text.Trim()); ds = ctl.Globe.DataManager.GetDataSourceByName(txt_ip.Text.Trim() + "/" + txt_db.Text.Trim() + "_" + txt_user.Text.Trim()); if (ds == null) { ds = ctl.Globe.DataManager.OpenOracleDataSource(txt_ip.Text.Trim() + "/" + txt_db.Text.Trim(), "", "", txt_user.Text.Trim(), txt_pwd.Text.Trim()); } if (ds == null) { ds = ctl.Globe.DataManager.CreateOracleDataSource(txt_ip.Text.Trim() + "/" + txt_db.Text.Trim(), "", "", txt_user.Text.Trim(), txt_pwd.Text.Trim()); } if (ds == null) { MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { dbServer = txt_ip.Text.Trim(); database = txt_db.Text.Trim(); userId = txt_user.Text.Trim(); password = txt_pwd.Text.Trim(); WriteXML(); } this.DialogResult = DialogResult.OK; MessageBox.Show("已成功连接数据库!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); } catch (Exception ex) { MessageBox.Show("数据库连接失败:" + ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void FrmFlagDatabase_Load(object sender, EventArgs e) { try { ReadXML(); } catch (Exception ex) { MessageBox.Show("窗体加载失败:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ReadXML() { string strFileName = Path.GetDirectoryName(Application.ExecutablePath) + "/EMSdatabaseConfig.xml"; if (!File.Exists(strFileName)) { return; } //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); //加载XML文件 myDoc.Load(strFileName); //搜索指定的节点 XmlNode serverRootNode = myDoc.SelectSingleNode("CyberPipe"); XmlNodeList nodes = null; if (serverRootNode != null) { nodes = myDoc.SelectSingleNode("CyberPipe").ChildNodes; if (nodes != null) { bool bRecorded = false; XmlNode bRecordNode = serverRootNode.SelectSingleNode("IsRecordedSql"); if (bRecordNode != null) { bool.TryParse(bRecordNode.InnerText, out bRecorded); chk.Checked = bRecorded; } if (bRecorded) { foreach (System.Xml.XmlNode xn in nodes) { if (xn.Name == "serverIP") { txt_ip.Text = xn.InnerText; } else if (xn.Name == "pipedb") { txt_db.Text = xn.InnerText; } else if (xn.Name == "userName") { txt_user.Text = xn.InnerText; } else if (xn.Name == "password") { txt_pwd.Text = xn.InnerText; } } } } } } private void WriteXML() { string strFileName = Path.GetDirectoryName(Application.ExecutablePath) + "/EMSdatabaseConfig.xml"; File.WriteAllText(strFileName, "<?xml version='1.0' encoding='utf-8' ?><CyberPipe></CyberPipe> "); XmlDocument myDoc = new XmlDocument(); //加载XML文件 // XmlElement serverRootNode = null; try { myDoc.Load(strFileName); } catch (Exception e) { File.Delete(strFileName); File.WriteAllText(strFileName, "<?xml version='1.0' encoding='utf-8' ?><CyberPipe></CyberPipe> "); throw e; } XmlNode serverRootNode = myDoc.SelectSingleNode("CyberPipe"); if (serverRootNode == null) { serverRootNode = myDoc.CreateElement("CyberPipe"); } XmlElement ele0 = myDoc.CreateElement("IsRecordedSql"); ele0.InnerText = chk.Checked.ToString(); serverRootNode.AppendChild(ele0); XmlElement ele1 = myDoc.CreateElement("serverIP"); ele1.InnerText = dbServer; serverRootNode.AppendChild(ele1); XmlElement ele2 = myDoc.CreateElement("pipedb"); ele2.InnerText = database; serverRootNode.AppendChild(ele2); XmlElement ele3 = myDoc.CreateElement("userName"); ele3.InnerText = userId; serverRootNode.AppendChild(ele3); XmlElement ele4 = myDoc.CreateElement("password"); ele4.InnerText = password; serverRootNode.AppendChild(ele4); try { myDoc.Save(strFileName); } catch (Exception exp) { throw exp; } } } }