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.Data; using GeoScene.Globe; using GeoScene.Engine; using System.Data.SqlClient; using System.IO; using System.Xml; namespace Cyberpipe { public partial class FrmDataBaseOpt : Office2007Form { public static GeoScene.Engine.GSODataSource ds = null; GSOGlobeControl globeControl2; public static string dbServer = ""; public static string database = ""; public static string userId = ""; public static string password = ""; public static SqlConnection conn = null; public static List<string> listLayerCaption = new List<string>(); public static List<string> listIpAndDatabase = new List<string>(); public static string layerIP = ""; public static string layerDatabase = ""; public FrmDataBaseOpt(GSOGlobeControl ctl) { InitializeComponent(); globeControl2 = ctl; } /// <summary> /// 连接按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonOk_Click(object sender, EventArgs e) { if (textBoxIP.Text == "" || textBoxPort.Text == "" || textBoxUser.Text == "" || textBoxPsw.Text == "") { MessageBox.Show("连接参数不能为空!", "提示"); return; } if (!Utility.isNetworkConnectionSuccess(textBoxIP.Text.Trim())) { MessageBox.Show("网络连接失败!", "提示"); return; } ds = globeControl2.Globe.DataManager.OpenOracleDataSource(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(); } listBox1.Items.Clear(); for (int i = 0; i < ds.DatasetCount; i++) { GSODataset dataset = ds.GetDatasetAt(i); if (dataset.Name.ToLower().Contains("network")) { continue; } listBox1.Items.Add(dataset.Name); } buttonX1.Enabled = true; WriteXML(); } /// <summary> /// 确定按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonX1_Click(object sender, EventArgs e) { if (listBox1.SelectedItems.Count > 0) { if (!Utility.isNetworkConnectionSuccess(dbServer)) { MessageBox.Show("网络连接失败!", "提示"); return; } if (layerIP != "" && layerDatabase != "") { if (layerIP != dbServer || layerDatabase != database) { listIpAndDatabase.Add(layerIP); listIpAndDatabase.Add(layerDatabase); layerIP = dbServer; layerDatabase = database; listLayerCaption.Add("--"); } } else { layerIP = dbServer; layerDatabase = database; } for (int i = 0; i < ds.DatasetCount; i++) { GSODataset dataset = ds.GetDatasetAt(i); if (dataset != null) { dataset.Caption = dataset.Name; GSOLayer layer = globeControl2.Globe.Layers.Add(dataset); if (layer != null) { layer.MaxVisibleAltitude = 7000; layer.Visible = listBox1.SelectedItems.Contains(dataset.Caption); listLayerCaption.Add(layer.Caption); } } } this.Close(); } else { Close(); } } /// <summary> /// 取消按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonX3_Click(object sender, EventArgs e) { Close(); } /// <summary> /// 窗体初始化事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmDataBaseOpt_Load(object sender, EventArgs e) { buttonX1.Enabled = false; ReadXML(); } /// <summary> /// 从配置文件读取数据库参数配置 /// </summary> private void ReadXML() { string strFileName = Application.StartupPath + "/databaseConfigGlobe2.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 + "/databaseConfigGlobe2.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); } } } }