private void button1_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; if (textBoxIP.Text == "" || textBoxDatabase.Text == "" || textBoxUser.Text == "" || textBoxPassword.Text == "") { this.Cursor = Cursors.Default; MessageBox.Show("连接参数不能为空!", "提示"); return; } if (!Utility.isNetworkConnectionSuccess(textBoxIP.Text.Trim())) { this.Cursor = Cursors.Default; MessageBox.Show("网络连接失败!", "提示"); return; } string serverIp = textBoxIP.Text.Trim(); string hostName = System.Net.Dns.GetHostName(); if (serverIp == hostName || serverIp == "localhost" || serverIp == System.Net.Dns.GetHostAddresses(hostName).GetValue(2).ToString()) { // serverIp = "127.0.0.1"; } DateTime timeStart = DateTime.Now; GSODataSource ds = globeControl1.Globe.DataManager.GetDataSourceByName(serverIp + "_" + textBoxDatabase.Text.Trim()); if (ds == null) { ds = globeControl1.Globe.DataManager.OpenSqlServerDataSource(serverIp, "", textBoxDatabase.Text, textBoxUser.Text, textBoxPassword.Text); } if (ds == null) { ds = globeControl1.Globe.DataManager.CreateSqlServerDataSource(serverIp, "", textBoxDatabase.Text, textBoxUser.Text, textBoxPassword.Text); } TimeSpan timeConnectSqlServer = DateTime.Now - timeStart; double secondsConnectSqlServer = timeConnectSqlServer.TotalSeconds; if (ds == null) { this.Cursor = Cursors.Default; MessageBox.Show("SqlServer数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { ds.IsCloseSaved = false; DatabaseConnectParams connectParams = new DatabaseConnectParams(); connectParams.ip = textBoxIP.Text.Trim(); connectParams.databaseName = textBoxDatabase.Text.Trim(); connectParams.userName = textBoxUser.Text.Trim(); connectParams.password = textBoxPassword.Text.Trim(); connectParams.dataSourceName = ds.Name; connectParams.databaseType = EnumDataSourceType.SqlServer; Utility.connectParamsOfDatabase.Add(connectParams); WriteXML(); MessageBox.Show("SqlServer数据库连接成功,用时:" + secondsConnectSqlServer.ToString() + "秒", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } this.DialogResult = DialogResult.OK; this.Cursor = Cursors.Default; this.Close(); }
public static int ExecuteScalar(string sql, DatabaseConnectParams connectParams) { SqlConnection conn = OledbHelper.getSqlConnection(connectParams); if (conn == null) { return 0; } SqlCommand sqlCmd = conn.CreateCommand(); sqlCmd.CommandText = sql; int num = 0; try { conn.Open(); num = Convert.ToInt32(sqlCmd.ExecuteScalar().ToString()); } catch (Exception ex) { Log.PublishTxt(ex); num = -1; MessageBox.Show(ex.Message, "提示"); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } return num; }
public static int ExecuteNonQuery(string cmdText, DatabaseConnectParams connectParams) { OleDbConnection oleConn = new OleDbConnection("Data Source=" + connectParams.ip + ";Initial Catalog=" + connectParams.databaseName + ";Persist Security Info=True;User ID=" + connectParams.userName + ";pwd=" + connectParams.password + ""); OleDbCommand oleCmd = new OleDbCommand(); oleCmd.Connection = oleConn; oleCmd.CommandText = cmdText; int rowCount = 0; try { oleConn.Open(); rowCount = oleCmd.ExecuteNonQuery(); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); } finally { if (oleConn.State == ConnectionState.Open) { oleConn.Close(); } } return rowCount; }
private void Frm_LogManager_Load(object sender, EventArgs e) { dataGridView1.ReadOnly = true; this.datetime1.Checked = false; this.datetime2.Checked = false; comboBoxDataSourceList.Items.Clear(); foreach (DatabaseConnectParams connectParams in Utility.connectParamsOfDatabase) { if (connectParams != null && (connectParams.databaseType == EnumDataSourceType.SqlServer || connectParams.databaseType == EnumDataSourceType.Oracle)) { comboBoxDataSourceList.Items.Add(connectParams.dataSourceFullName); } } if (comboBoxDataSourceList.Items.Count > 0) { comboBoxDataSourceList.SelectedIndex = 0; connectParams = Utility.getConnectParamsByDatasourceFullName(globeControl1, comboBoxDataSourceList.SelectedItem.ToString().Trim()); if (connectParams != null) { string sql = "use " + connectParams.databaseName + " select * from 日志管理;"; DataTable table = OledbHelper.QueryTable(sql, connectParams); if (table == null) { MessageBox.Show("数据库中没有日志信息!", "提示"); return; } dataGridView1.DataSource = table; } } }
private void comboBoxDataSourceList_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxDataSourceList.SelectedItem != null) { connectParams = Utility.getConnectParamsByDatasourceFullName(globeControl1, comboBoxDataSourceList.SelectedItem.ToString().Trim()); } }
public static int sqlExecuteNonQuery(string sql, DatabaseConnectParams connectParams) { SqlConnection conn = OledbHelper.getSqlConnection(connectParams); if (conn == null) { return(0); } SqlCommand sqlCmd = conn.CreateCommand(); sqlCmd.CommandText = sql; int rowCount = 0; try { conn.Open(); rowCount = sqlCmd.ExecuteNonQuery(); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } return(rowCount); }
public static int ExecuteNonQuery(string cmdText, DatabaseConnectParams connectParams) { OleDbConnection oleConn = new OleDbConnection("Data Source=" + connectParams.ip + ";Initial Catalog=" + connectParams.databaseName + ";Persist Security Info=True;User ID=" + connectParams.userName + ";pwd=" + connectParams.password + ""); OleDbCommand oleCmd = new OleDbCommand(); oleCmd.Connection = oleConn; oleCmd.CommandText = cmdText; int rowCount = 0; try { oleConn.Open(); rowCount = oleCmd.ExecuteNonQuery(); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); } finally { if (oleConn.State == ConnectionState.Open) { oleConn.Close(); } } return(rowCount); }
public static int ExecuteScalar(string sql, DatabaseConnectParams connectParams) { SqlConnection conn = OledbHelper.getSqlConnection(connectParams); if (conn == null) { return(0); } SqlCommand sqlCmd = conn.CreateCommand(); sqlCmd.CommandText = sql; int num = 0; try { conn.Open(); num = Convert.ToInt32(sqlCmd.ExecuteScalar().ToString()); } catch (Exception ex) { Log.PublishTxt(ex); num = -1; MessageBox.Show(ex.Message, "提示"); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } return(num); }
private bool backupDB() { DatabaseConnectParams connectParams = Utility.getConnectParamsByDatasourceFullName(globeControl1, comboBoxDataSourceList.SelectedItem.ToString().Trim()); if (connectParams == null) { return(false); } conn = OledbHelper.getSqlConnection(connectParams); SqlCommand cmdBK = new SqlCommand(); cmdBK.CommandType = CommandType.Text; cmdBK.Connection = conn; DateTime currentTime = DateTime.Now; string Dtime = currentTime.GetDateTimeFormats('D')[0].ToString(); string Htime = DateTime.Now.ToString("HH时mm分ss秒").Trim(); string fileName = Dtime + "(" + Htime + ")"; string DBBackupPath = Application.StartupPath + "\\" + comboBoxDataSourceList.SelectedItem.ToString().Trim(); if (Directory.Exists(DBBackupPath) == false) { Directory.CreateDirectory(DBBackupPath); } string pathName = DBBackupPath + "\\" + fileName + ".bak"; string databaseName = connectParams.databaseName; cmdBK.CommandText = "backup database " + databaseName + " to disk='" + pathName + "' with init"; try { if (conn.State == ConnectionState.Closed) { conn.Open(); } cmdBK.ExecuteNonQuery(); return(true); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); return(false); } finally { conn.Close(); } }
public static SqlConnection getSqlConnection(DatabaseConnectParams connectParams) { if (connectParams == null) { return(null); } try { SqlConnection conn = new SqlConnection("Server=" + connectParams.ip + ";Database=" + connectParams.databaseName + ";User ID=" + connectParams.userName + ";pwd=" + connectParams.password + ";Trusted_Connection=false"); return(conn); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); return(null); } }
public static DataTable QueryTable(string cmdText, DatabaseConnectParams connectParams) { SqlConnection conn = OledbHelper.getSqlConnection(connectParams); if (conn == null) { return(null); } try { conn.Open(); SqlCommand sqlCmd = conn.CreateCommand(); sqlCmd.CommandText = cmdText; SqlDataReader sqlReader = sqlCmd.ExecuteReader(); DataTable table = new DataTable(); table.Load(sqlReader); sqlReader.Close(); conn.Close(); return(table); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); return(null); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } }
private void cbox_Layers_SelectedIndexChanged(object sender, EventArgs e) { GSOLayer m_layer = globeControl1.Globe.Layers.GetLayerByCaption(cbox_Layers.SelectedItem.ToString().Trim());//获取当前选择的layer图层 if (m_layer == null || m_layer.Dataset == null || m_layer.Dataset.DataSource == null) { return; } connectParams = Utility.getConnectParamsByDatasourceName(globeControl1, m_layer.Dataset.DataSource.Name); if (connectParams == null) { return; } listBox_Field.Items.Clear(); GSOFeatureLayer flayer = m_layer as GSOFeatureLayer; sourcefDataset = m_layer.Dataset as GSOFeatureDataset; if (sourcefDataset != null) { sourcefDataset.Open(); for (int j = 0; j < sourcefDataset.FieldCount; j++) { GSOFieldAttr fieldef = sourcefDataset.GetField(j); listBox_Field.Items.Add(fieldef.Name); } //设置当前选择字段为第一个 if (listBox_Field.Items.Count > 0) { listBox_Field.SelectedIndex = 0; } //将描述信息修改 label3.Text = "SELECT * FROM " + sourcefDataset.Name + " WHERE:"; } }
public static SqlConnection getSqlConnection(DatabaseConnectParams connectParams) { if (connectParams == null) { return null; } try { SqlConnection conn = new SqlConnection("Server=" + connectParams.ip + ";Database=" + connectParams.databaseName + ";User ID=" + connectParams.userName + ";pwd=" + connectParams.password + ";Trusted_Connection=false"); return conn; } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); return null; } }
private void btnEnter_Click(object sender, EventArgs e) { int n = 0; layer = globeControl1.Globe.Layers.GetLayerByCaption(cmbPipeShow.Text.Trim()); GSOFeatureLayer flayer = layer as GSOFeatureLayer; if(flayer == null) { return ; } GSOFeatureDataset fdataset = flayer.Dataset as GSOFeatureDataset; if (fdataset == null || fdataset.DataSource == null) { return; } connectParams = Utility.getConnectParamsByDatasourceName(globeControl1, fdataset.DataSource.Name); if (connectParams == null) { return; } GSOFeatures feats = flayer.GetAllFeatures(); string type = layer.Caption.Substring(0, 2); string sql = "select * " + " from " + layer.Caption + " where"; for (int i = 0; i < feats.Length; i++) { GSOFeature f = feats[i]; if (f.Geometry.Type == EnumGeometryType.GeoPolyline3D) { GSOGeoPolyline3D line = f.Geometry as GSOGeoPolyline3D; GSOPipeLineStyle3D style = line.Style as GSOPipeLineStyle3D; if (style != null) { GSOPoint3ds pnts = getAllPointInPipeline(line); for (int j = 0; j < pnts.Count; j++) { GSOPoint3d pt = pnts[j]; if (Math.Abs(pt.Z) < style.Radius) { n++; sql += " 编号='" + f.Name + "' or "; break; } } } } } if (n > 0) { sql = sql.Substring(0, sql.Length - 3); DataTable dt = new DataTable(); dt = OledbHelper.QueryTable(sql, connectParams); if (dt == null) { MessageBox.Show("没有地上管线!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (dt.Columns.Count > 3) { dt.Columns.RemoveAt(0); dt.Columns.RemoveAt(0); dt.Columns.RemoveAt(0); } dataGridView1.ReadOnly = !layer.Editable; dataGridView1.DataSource = dt; if (statusStrip1.Items.Count > 0) { statusStrip1.Items[0].Text = " 共有 " + dt.Rows.Count + " 条记录"; } globeControl1.Globe.Refresh(); } } else { MessageBox.Show("没有地上管线!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); dataGridView1.DataSource = null; if (statusStrip1.Items.Count > 0) { statusStrip1.Items[0].Text = " 共有 " + 0 + " 条记录"; } } }
public static DataTable QueryTable(string cmdText,DatabaseConnectParams connectParams) { SqlConnection conn = OledbHelper.getSqlConnection(connectParams); if (conn == null) { return null; } try { conn.Open(); SqlCommand sqlCmd = conn.CreateCommand(); sqlCmd.CommandText = cmdText; SqlDataReader sqlReader = sqlCmd.ExecuteReader(); DataTable table = new DataTable(); table.Load(sqlReader); sqlReader.Close(); conn.Close(); return table; } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); return null; } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } }
public static int sqlExecuteNonQuery(string sql, DatabaseConnectParams connectParams) { SqlConnection conn = OledbHelper.getSqlConnection(connectParams); if (conn == null) { return 0; } SqlCommand sqlCmd = conn.CreateCommand(); sqlCmd.CommandText = sql; int rowCount = 0; try { conn.Open(); rowCount = sqlCmd.ExecuteNonQuery(); } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } } return rowCount; }
//开始查询 private void btn_Apply_Click(object sender, EventArgs e) { string caption = cbox_Layers.Text; if (caption == "") { MessageBox.Show("请先添加图层", "提示"); return; } GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(caption); if (layer == null) { return; } if (!checkBox1.Checked) { GSOFeatures features = layer.GetAllFeatures(); for (int i = 0; i < features.Length; i++) { GSOFeature newfeature = features[i]; GSOGeoPolyline3D line = newfeature.Geometry as GSOGeoPolyline3D; if (line == null) { continue; } GSOPipeLineStyle3D pipeLineStyle = line.Style as GSOPipeLineStyle3D; // 管线暂不支持依地模式 if (pipeLineStyle == null) { pipeLineStyle = new GSOPipeLineStyle3D(); } pipeLineStyle.LineColor = buttonPipelineColor.BackColor; line.Style = pipeLineStyle; } } else { string sql = "select * from " + cbox_Layers.Text.Trim(); if (textBox_WhereClause.Text != "") { sql += " where " + textBox_WhereClause.Text; try { if (layer.Dataset == null || layer.Dataset.DataSource == null || (layer.Dataset.DataSource.Type != EnumDataSourceType.SqlServer && layer.Dataset.DataSource.Type != EnumDataSourceType.Oracle)) { return; } DatabaseConnectParams connectParams = Utility.getConnectParamsByDatasourceName(globeControl1, layer.Dataset.DataSource.Name); if (connectParams == null) { return; } DataTable table = OledbHelper.QueryTable(sql, connectParams); if (table == null) { return; } for (int i = 0; i < table.Rows.Count; i++) { int id = Convert.ToInt32(table.Rows[i]["LSSYS_ID"]); GSOFeature newfeature = layer.GetFeatureByID(id); GSOGeoPolyline3D line = newfeature.Geometry as GSOGeoPolyline3D; if (line == null) { continue; } GSOPipeLineStyle3D pipeLineStyle = line.Style as GSOPipeLineStyle3D; // 管线暂不支持依地模式 if (pipeLineStyle == null) { pipeLineStyle = new GSOPipeLineStyle3D(); } pipeLineStyle.LineColor = buttonPipelineColor.BackColor; line.Style = pipeLineStyle; } } catch (Exception ex) { Log.PublishTxt(ex); MessageBox.Show(ex.Message, "提示"); return; } } } globeControl1.Refresh(); return; }
private void cbox_Layers_SelectedIndexChanged(object sender, EventArgs e) { GSOLayer m_layer = globeControl1.Globe.Layers.GetLayerByCaption(cbox_Layers.SelectedItem.ToString().Trim());//获取当前选择的layer图层 if (m_layer == null || m_layer.Dataset == null || m_layer.Dataset.DataSource == null) return; connectParams = Utility.getConnectParamsByDatasourceName(globeControl1, m_layer.Dataset.DataSource.Name); if (connectParams == null) { return; } listBox_Field.Items.Clear(); GSOFeatureLayer flayer = m_layer as GSOFeatureLayer; sourcefDataset = m_layer.Dataset as GSOFeatureDataset; if (sourcefDataset != null) { sourcefDataset.Open(); for (int j = 0; j < sourcefDataset.FieldCount; j++) { GSOFieldAttr fieldef = sourcefDataset.GetField(j); listBox_Field.Items.Add(fieldef.Name); } //设置当前选择字段为第一个 if (listBox_Field.Items.Count > 0) { listBox_Field.SelectedIndex = 0; } //将描述信息修改 label3.Text = "SELECT * FROM " + sourcefDataset.Name + " WHERE:"; } }
private void Frm_LogManager_Load(object sender, EventArgs e) { dataGridView1.ReadOnly = true; this.datetime1.Checked = false; this.datetime2.Checked = false; comboBoxDataSourceList.Items.Clear(); foreach (DatabaseConnectParams connectParams in Utility.connectParamsOfDatabase) { if (connectParams != null && (connectParams.databaseType == EnumDataSourceType.SqlServer || connectParams.databaseType == EnumDataSourceType.Oracle)) { comboBoxDataSourceList.Items.Add(connectParams.dataSourceFullName); } } if (comboBoxDataSourceList.Items.Count > 0) { comboBoxDataSourceList.SelectedIndex = 0; connectParams = Utility.getConnectParamsByDatasourceFullName(globeControl1, comboBoxDataSourceList.SelectedItem.ToString().Trim()); if (connectParams != null) { string sql = "use " + connectParams.databaseName + " select * from 日志管理;"; DataTable table = OledbHelper.QueryTable(sql,connectParams); if (table == null) { MessageBox.Show("数据库中没有日志信息!", "提示"); return; } dataGridView1.DataSource = table; } } }
private void button1_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; if (textBoxIP.Text == "" || textBoxDatabase.Text == "" || textBoxUser.Text == "" || textBoxPassword.Text == "") { this.Cursor = Cursors.Default; MessageBox.Show("连接参数不能为空!", "提示"); return; } if (!Utility.isNetworkConnectionSuccess(textBoxIP.Text.Trim())) { this.Cursor = Cursors.Default; MessageBox.Show("网络连接失败!", "提示"); return; } string serverIp = textBoxIP.Text.Trim(); string hostName = System.Net.Dns.GetHostName(); if (serverIp == hostName || serverIp == "localhost" || serverIp == System.Net.Dns.GetHostAddresses(hostName).GetValue(2).ToString()) { serverIp = "127.0.0.1"; } DateTime timeStart = DateTime.Now; GSODataSource ds = globeControl1.Globe.DataManager.GetDataSourceByName(serverIp + "/" + textBoxDatabase.Text.Trim() + "_" + textBoxUser.Text.Trim()); if (ds == null) { ds = globeControl1.Globe.DataManager.OpenOracleDataSource(serverIp + "/" + textBoxDatabase.Text, "", "", textBoxUser.Text, textBoxPassword.Text); } if (ds == null) { ds = globeControl1.Globe.DataManager.CreateOracleDataSource(serverIp + "/" + textBoxDatabase.Text, "", "", textBoxUser.Text, textBoxPassword.Text); } TimeSpan timeConnectOracle = DateTime.Now - timeStart; double secondsConnectOracle = timeConnectOracle.TotalSeconds; if (ds == null) { this.Cursor = Cursors.Default; MessageBox.Show("连接Oracle数据库失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { ds.IsCloseSaved = false; DatabaseConnectParams connectParams = new DatabaseConnectParams(); connectParams.ip = textBoxIP.Text.Trim(); connectParams.databaseName = textBoxDatabase.Text.Trim(); connectParams.userName = textBoxUser.Text.Trim(); connectParams.password = textBoxPassword.Text.Trim(); connectParams.dataSourceName = ds.Name; connectParams.databaseType = EnumDataSourceType.Oracle; Utility.connectParamsOfDatabase.Add(connectParams); WriteXML(); MessageBox.Show("连接Oracle数据库成功,用时:" + secondsConnectOracle.ToString() + "秒", "提示"); } this.DialogResult = DialogResult.OK; this.Cursor = Cursors.Default; this.Close(); }
private void btnEnter_Click(object sender, EventArgs e) { int n = 0; layer = globeControl1.Globe.Layers.GetLayerByCaption(cmbPipeShow.Text.Trim()); GSOFeatureLayer flayer = layer as GSOFeatureLayer; if (flayer == null) { return; } GSOFeatureDataset fdataset = flayer.Dataset as GSOFeatureDataset; if (fdataset == null || fdataset.DataSource == null) { return; } connectParams = Utility.getConnectParamsByDatasourceName(globeControl1, fdataset.DataSource.Name); if (connectParams == null) { return; } GSOFeatures feats = flayer.GetAllFeatures(); string type = layer.Caption.Substring(0, 2); string sql = "select * " + " from " + layer.Caption + " where"; for (int i = 0; i < feats.Length; i++) { GSOFeature f = feats[i]; if (f.Geometry.Type == EnumGeometryType.GeoPolyline3D) { GSOGeoPolyline3D line = f.Geometry as GSOGeoPolyline3D; GSOPipeLineStyle3D style = line.Style as GSOPipeLineStyle3D; if (style != null) { GSOPoint3ds pnts = getAllPointInPipeline(line); for (int j = 0; j < pnts.Count; j++) { GSOPoint3d pt = pnts[j]; if (Math.Abs(pt.Z) < style.Radius) { n++; sql += " 编号='" + f.Name + "' or "; break; } } } } } if (n > 0) { sql = sql.Substring(0, sql.Length - 3); DataTable dt = new DataTable(); dt = OledbHelper.QueryTable(sql, connectParams); if (dt == null) { MessageBox.Show("没有地上管线!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (dt.Columns.Count > 3) { dt.Columns.RemoveAt(0); dt.Columns.RemoveAt(0); dt.Columns.RemoveAt(0); } dataGridView1.ReadOnly = !layer.Editable; dataGridView1.DataSource = dt; if (statusStrip1.Items.Count > 0) { statusStrip1.Items[0].Text = " 共有 " + dt.Rows.Count + " 条记录"; } globeControl1.Globe.Refresh(); } } else { MessageBox.Show("没有地上管线!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); dataGridView1.DataSource = null; if (statusStrip1.Items.Count > 0) { statusStrip1.Items[0].Text = " 共有 " + 0 + " 条记录"; } } }