using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using GeoScene.Data; using GeoScene.Engine; using GeoScene.Globe; using System.Data.SqlClient; using System.IO; using System.Xml; namespace Cyberpipe { public partial class FrmDatabaseParaSetting :DevComponents.DotNetBar.Office2007Form { public static string dbServer = ""; public static string database = ""; public static string userId = ""; public static string password = ""; private GSOGlobeControl ctl; public FrmDatabaseParaSetting(GSOGlobeControl _ctl) { ctl = _ctl; InitializeComponent(); } /// <summary> /// 窗体初始化事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmDatabaseParaSetting_Load(object sender, EventArgs e) { ReadXML(); } public static GeoScene.Engine.GSODataSource ds = null; /// <summary> /// 确定按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if (textBoxIP.Text == "" || textBoxUser.Text == "" || textBoxPsw.Text == "") { MessageBox.Show("连接参数不能为空!", "提示"); return; } //if (!Utility.isNetworkConnectionSuccess(textBoxIP.Text.Trim())) //{ // MessageBox.Show("网络连接失败!", "提示"); // return; //} ds = ctl.Globe.DataManager.OpenOracleDataSource(textBoxIP.Text, "", "", textBoxUser.Text, textBoxPsw.Text); //if (ds == null) // ds = ctl.Globe.DataManager.CreateSqlServerDataSource(textBoxIP.Text, "", textBoxPort.Text, textBoxUser.Text, textBoxPsw.Text); if (ds == null) { MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { dbServer = textBoxIP.Text.Trim(); database = textBoxPort.Text.Trim(); userId = textBoxUser.Text.Trim(); password = textBoxPsw.Text.Trim(); WriteXML(); } this.DialogResult = DialogResult.OK; this.Close(); } /// <summary> /// 从配置文件读取数据库连接参数配置 /// </summary> private void ReadXML() { string strFileName = Application.StartupPath + "/databaseConfig.xml"; if (!File.Exists(strFileName)) { return; } //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); { //加载XML文件 try { myDoc.Load(strFileName); } catch (System.Exception e) { LogError.PublishError(e); return; } //搜索指定的节点 XmlNode serverRootNode = myDoc.SelectSingleNode("LocaSpace"); XmlNodeList nodes = null; if (serverRootNode != null) { nodes = myDoc.SelectSingleNode("LocaSpace").ChildNodes; } if (nodes != null) { XmlNode bRecordNode = serverRootNode.SelectSingleNode("IsRecordedSql"); bool bRecorded = false; if (bRecordNode != null) { bool.TryParse(bRecordNode.InnerText, out bRecorded); cbbRecordDatabaseConfig.Checked = bRecorded; } if (bRecorded) { foreach (System.Xml.XmlNode xn in nodes) { if (xn.Name == "sqlIP") { textBoxIP.Text = xn.InnerText; } else if (xn.Name == "database") { textBoxPort.Text = xn.InnerText; } else if (xn.Name == "userName") { textBoxUser.Text = xn.InnerText; } else if (xn.Name == "password") { textBoxPsw.Text = xn.InnerText; } } } } } } /// <summary> /// 向配置文件写入数据库参数配置 /// </summary> private void WriteXML() { string strFileName = Application.StartupPath + "/databaseConfig.xml"; File.WriteAllText(strFileName, "<?xml version='1.0' encoding='utf-8' ?><LocaSpace></LocaSpace> "); XmlDocument myDoc = new XmlDocument(); //加载XML文件 // XmlElement serverRootNode = null; try { myDoc.Load(strFileName); } catch (System.Exception e) { LogError.PublishError(e); File.Delete(strFileName); File.WriteAllText(strFileName, "<?xml version='1.0' encoding='utf-8' ?><LocaSpace></LocaSpace> "); } XmlNode serverRootNode = myDoc.SelectSingleNode("LocaSpace"); if (serverRootNode == null) { serverRootNode = myDoc.CreateElement("LocaSpace"); } XmlElement ele0 = myDoc.CreateElement("IsRecordedSql"); ele0.InnerText = cbbRecordDatabaseConfig.Checked.ToString(); serverRootNode.AppendChild(ele0); XmlElement ele1 = myDoc.CreateElement("sqlIP"); ele1.InnerText = dbServer; serverRootNode.AppendChild(ele1); XmlElement ele2 = myDoc.CreateElement("database"); 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) { LogError.PublishError(exp); } } /// <summary> /// 取消按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonNo_Click(object sender, EventArgs e) { this.Close(); } } }