/// <summary> /// 通过连接获取DataSource /// </summary> /// <param name="ci"></param> /// <returns></returns> private IDataSource GetDataSource(IConnectionInfo ci) { if (dsFactory == null) { dsFactory = new DataSourceFactory(); } IDataSource ds = dsFactory.OpenDataSource(ci); return(ds); }
private bool CanOpen(String dsrc, IConnectionInfo info) { try { IDataSource ds = dataSourceFactory.OpenDataSource(info); if (ds != null) { //System.Runtime.InteropServices.//Marshal.ReleaseComObject(ds); ds = null; return(true); } else { return(false); } } catch (Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } return(false); } }
private void FeatureDataSetMapToFeatureClassMap() { try { if (_ci == null) { return; } dsFactory = new DataSourceFactory(); IDataSource ds = dsFactory.OpenDataSource(_ci); string[] setnames = (string[])ds.GetFeatureDatasetNames(); if (setnames == null || setnames.Length == 0) { return; } foreach (string name in setnames) { IFeatureDataSet dataSet = ds.OpenFeatureDataset(name); string[] fcnames = (string[])dataSet.GetNamesByType(i3dDataSetType.i3dDataSetFeatureClassTable); if (fcnames == null || fcnames.Length == 0) { continue; } foreach (string fcname in fcnames) { IFeatureClass fc = dataSet.OpenFeatureClass(fcname); if (fc != null) { List <string> geoNames = new List <string>(); IFieldInfoCollection fieldinfos = fc.GetFields(); if (fieldinfos != null && fieldinfos.Count > 0) { for (int i = 0; i < fieldinfos.Count; i++) { IFieldInfo field = fieldinfos.Get(i); if (field != null && field.GeometryDef != null) { geoNames.Add(field.Name); } } _featureClassMapping.Add(fc, geoNames); } } } } } catch (Exception ex) { LoggerHelper.Logger.Error(ex, "执行FeatureDataSetMapToFeatureClassMap错误"); } }
private void FeatureDataSetMapToFeatureClass() { try { if (ci == null) { return; } dsFactory = new DataSourceFactory(); IDataSource ds = dsFactory.OpenDataSource(ci); string[] setnames = (string[])ds.GetFeatureDatasetNames(); if (setnames == null || setnames.Length == 0) { return; } featureDataSetMapping = new Dictionary <IFeatureDataSet, List <IFeatureClass> >(setnames.Length); foreach (string name in setnames) { IFeatureDataSet dataSet = ds.OpenFeatureDataset(name); string[] fcnames = (string[])dataSet.GetNamesByType(i3dDataSetType.i3dDataSetFeatureClassTable); if (fcnames == null || fcnames.Length == 0) { continue; } featureClasses = new List <IFeatureClass>(fcnames.Length); foreach (string fcname in fcnames) { IFeatureClass fc = dataSet.OpenFeatureClass(fcname); if (fc != null) { featureClasses.Add(fc); } } featureDataSetMapping.Add(dataSet, featureClasses); } } catch (Exception ex) { throw ex; } }
// 公共方法 private void bindDataToCatalogTree(IConnectionInfo ci) { try { if (dsFactory == null) { dsFactory = new DataSourceFactory(); } IDataSource ds = dsFactory.OpenDataSource(ci); myTreeNode sourceNode = null; if (ci.ConnectionType == gviConnectionType.gviConnectionMySql5x) { sourceNode = new myTreeNode(ci.Database + "@" + ci.Server, ci); } else { sourceNode = new myTreeNode(ci.Database, ci); } this.treeView1.Nodes.Add(sourceNode); // 获取dataset string[] setnames = (string[])ds.GetFeatureDatasetNames(); if (setnames.Length == 0) { return; } foreach (string setname in setnames) { IFeatureDataSet dataset = ds.OpenFeatureDataset(setname); TreeNode setNode = new TreeNode(setname, 1, 1); sourceNode.Nodes.Add(setNode); // 获取featureclass string[] fcnames = (string[])dataset.GetNamesByType(gviDataSetType.gviDataSetFeatureClassTable); if (fcnames == null || fcnames.Length == 0) { continue; } foreach (string fcname in fcnames) { IFeatureClass fc = dataset.OpenFeatureClass(fcname); TreeNode fcNode = new TreeNode(fcname, 2, 2); fcNode.ContextMenuStrip = this.contextMenuStrip2; setNode.Nodes.Add(fcNode); // 获取属性字段 IFieldInfoCollection fieldinfos = fc.GetFields(); for (int i = 0; i < fieldinfos.Count; i++) { IFieldInfo fieldinfo = fieldinfos.Get(i); if (null == fieldinfo) { continue; } TreeNode fieldinfoNode = new TreeNode(fieldinfo.Name); fieldinfoNode.ContextMenuStrip = this.contextMenuStrip1; // 绑定右键菜单 fcNode.Nodes.Add(fieldinfoNode); } } } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); return; } }
/// <summary> /// 添加数据源 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addDatasourceToolStripButton_Click(object sender, EventArgs e) { OpenDataSourceForm dsForm = new OpenDataSourceForm(false); if (dsForm.ShowDialog() != DialogResult.OK) { return; } IConnectionInfo ci = new ConnectionInfo(); switch (dsForm.ConnectionType) { case "gviConnectionMySql5x": ci.ConnectionType = gviConnectionType.gviConnectionMySql5x; break; case "gviConnectionFireBird2x": ci.ConnectionType = gviConnectionType.gviConnectionFireBird2x; break; case "gviConnectionSQLite3": ci.ConnectionType = gviConnectionType.gviConnectionSQLite3; break; } ci.Server = dsForm.Server; ci.Port = dsForm.Port; ci.Database = dsForm.Database; ci.UserName = dsForm.UserName; ci.Password = dsForm.Password; IDataSource ds = null; IFeatureDataSet dataset = null; IFeatureClass fc = null; try { if (dsFactory == null) { dsFactory = new DataSourceFactory(); } ds = dsFactory.OpenDataSource(ci); myTreeNode sourceNode = null; if (ci.ConnectionType == gviConnectionType.gviConnectionMySql5x) { sourceNode = new myTreeNode(ci.Database + "@" + ci.Server, ci); } else { sourceNode = new myTreeNode(ci.Database, ci); } sourceNode.ContextMenuStrip = this.contextMenuStrip1; this.treeView1.Nodes.Add(sourceNode); // 获取dataset string[] setnames = (string[])ds.GetFeatureDatasetNames(); if (setnames.Length == 0) { return; } foreach (string setname in setnames) { dataset = ds.OpenFeatureDataset(setname); TreeNode setNode = new TreeNode(setname, 1, 1); setNode.ContextMenuStrip = this.contextMenuStrip2; sourceNode.Nodes.Add(setNode); // 获取featureclass string[] fcnames = (string[])dataset.GetNamesByType(gviDataSetType.gviDataSetFeatureClassTable); if (fcnames == null || fcnames.Length == 0) { continue; } foreach (string fcname in fcnames) { fc = dataset.OpenFeatureClass(fcname); TreeNode fcNode = new TreeNode(fcname, 2, 2); fcNode.ContextMenuStrip = this.contextMenuStrip3; setNode.Nodes.Add(fcNode); // 获取属性字段 IFieldInfoCollection fieldinfos = fc.GetFields(); for (int i = 0; i < fieldinfos.Count; i++) { IFieldInfo fieldinfo = fieldinfos.Get(i); if (null == fieldinfo) { continue; } TreeNode fieldinfoNode = new TreeNode(fieldinfo.Name); fieldinfoNode.ContextMenuStrip = this.contextMenuStrip4; // 绑定右键菜单 fcNode.Nodes.Add(fieldinfoNode); } } } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { if (ds != null) { //Marshal.ReleaseComObject(ds); ds = null; } if (dataset != null) { //Marshal.ReleaseComObject(dataset); dataset = null; } if (fc != null) { //Marshal.ReleaseComObject(fc); fc = null; } } }
private void toolStripButtonEditFeature_Click(object sender, EventArgs e) { this.dataGridView1.EndEdit(); //强制提交 if (dsFactory == null) { dsFactory = new DataSourceFactory(); } IDataSource ds = null; IFeatureDataSet dataset = null; IFeatureClass fc = null; IFdeCursor cursor = null; IFieldInfo field = null; try { if (dataGridView1.SelectedRows.Count == 1) { ds = dsFactory.OpenDataSource(Info.ci); dataset = ds.OpenFeatureDataset(Info.datasetName); fc = dataset.OpenFeatureClass(Info.featureclassName); // 比较是否修改了记录 int oid = int.Parse(dataGridView1.SelectedRows[0].Cells["oid"].Value.ToString()); IRowBuffer fdeRow = fc.GetRow(oid); bool isChanged = false; for (int i = 0; i < AttriTable.Columns.Count; ++i) { string strColName = AttriTable.Columns[i].ColumnName; int nPos = fdeRow.FieldIndex(strColName); if (nPos != -1) { field = fdeRow.Fields.Get(nPos); } if (nPos != -1 && strColName != "oid" && strColName != "GroupName" && field.FieldType != gviFieldType.gviFieldGeometry) { if (fdeRow.GetValue(nPos) == null || (!dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[i].EditedFormattedValue.Equals(fdeRow.GetValue(nPos).ToString()))) { isChanged = true; break; } } } if (!isChanged) { MessageBox.Show("该条记录无改动"); return; } for (int j = 0; j < AttriTable.Columns.Count; ++j) { string strColName = AttriTable.Columns[j].ColumnName; int nPos = fdeRow.FieldIndex(strColName); if (nPos != -1) { field = fdeRow.Fields.Get(nPos); } if (nPos != -1 && strColName != "oid" && strColName != "GroupName" && field.FieldType != gviFieldType.gviFieldGeometry) { fdeRow.SetValue(nPos, dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[j].EditedFormattedValue); //插入字段值 } } // 修改数据库中记录 IRowBufferCollection col = new RowBufferCollection(); col.Add(fdeRow); fc.UpdateRows(col, false); MessageBox.Show("修改成功"); } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { if (ds != null) { //Marshal.ReleaseComObject(ds); ds = null; } if (dataset != null) { //Marshal.ReleaseComObject(dataset); dataset = null; } if (fc != null) { //Marshal.ReleaseComObject(fc); fc = null; } if (cursor != null) { //Marshal.ReleaseComObject(cursor); cursor = null; } } }
/// <summary> /// 在要素类里删除要素 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButtonDeleteFeature_Click(object sender, EventArgs e) { if (dsFactory == null) { dsFactory = new DataSourceFactory(); } IDataSource ds = null; IFeatureDataSet dataset = null; IFeatureClass fc = null; try { if (dataGridView1.SelectedRows.Count > 0) { if (MessageBox.Show("该操作无法恢复,请确定是否删除所选择的要素?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { ds = dsFactory.OpenDataSource(Info.ci); dataset = ds.OpenFeatureDataset(Info.datasetName); fc = dataset.OpenFeatureClass(Info.featureclassName); int oid = int.Parse(dataGridView1.SelectedRows[0].Cells["oid"].Value.ToString()); // 从数据库中删除 IQueryFilter filter = new QueryFilter(); filter.WhereClause = "oid = " + oid; int nRet = fc.Delete(filter); MessageBox.Show("成功删除" + nRet + "条记录"); // 从界面DataTable中删除 int index = dataGridView1.SelectedRows[0].Index; AttriTable.Rows.RemoveAt(index); this.dataGridView1.Refresh(); if (index < dataGridView1.RowCount) { this.dataGridView1.Rows[index].Selected = true; this.dataGridView1.FirstDisplayedScrollingRowIndex = index; } else { this.dataGridView1.Rows[dataGridView1.RowCount - 1].Selected = true; this.dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1; } // 更新表头文字 if (FilterWhereClause.Equals("")) { this.Text = "Attributes of " + Info.featureclassName + " [Total records: " + fc.GetCount(null).ToString() + "]"; } else { this.Text = "Attributes of " + Info.featureclassName + " [Total records: " + AttriTable.Rows.Count.ToString() + "]" + " Filter: " + FilterWhereClause; } } } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { if (ds != null) { //Marshal.ReleaseComObject(ds); ds = null; } if (dataset != null) { //Marshal.ReleaseComObject(dataset); dataset = null; } if (fc != null) { //Marshal.ReleaseComObject(fc); fc = null; } } }