//connect to sde - method #region "Connect to SDE" public static ESRI.ArcGIS.Geodatabase.IWorkspace ConnectToTransactionalVersion(String server, String instance, String database, String authenication, String version) { IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("SERVER", server); //propertySet.SetProperty("DBCLIENT", dbclient); propertySet.SetProperty("INSTANCE", instance); propertySet.SetProperty("DATABASE", database); propertySet.SetProperty("AUTHENTICATION_MODE", authenication); propertySet.SetProperty("VERSION", version); Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); return(workspaceFactory.Open(propertySet, 0)); }
public bool ConnectToSDE() { if (!_IdentifySDE()) { return(false); } string SERVER = ConfigurationManager.AppSettings.Get("SDE_Server"); string INSTANCE = ConfigurationManager.AppSettings.Get("SDE_Instance"); string DATABASE = ConfigurationManager.AppSettings.Get("SDE_Database"); string USER = ConfigurationManager.AppSettings.Get("SDE_User"); string PASSWORD = ConfigurationManager.AppSettings.Get("SDE_Password"); string VERSION = ConfigurationManager.AppSettings.Get("SDE_Version"); IPropertySet property = new PropertySet(); property.SetProperty("SERVER", SERVER); property.SetProperty("INSTANCE", INSTANCE); property.SetProperty("DATABASE", DATABASE); property.SetProperty("USER", USER); property.SetProperty("PASSWORD", PASSWORD); property.SetProperty("VERSION", VERSION); try { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SDEWorkspaceFactory"); IWorkspaceFactory m_WorkSpaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); m_WorkSpace = m_WorkSpaceFactory.Open(property, 0); IVersion version = m_WorkSpace as IVersion; version.RefreshVersion(); if (m_WorkSpace == null) { return(false); } return(true); } catch (Exception ex) { string sr = ex.Message; return(false); } }
/// <summary> /// 打开连接 /// </summary> private IWorkspace getWorkspace() { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); //IWorkspaceFactory pSdeWorkspaceFactory = new SdeWorkspaceFactoryClass(); try { return(workspaceFactory.Open(GetConnectionProperty(), 0)); } catch (Exception ex) { XtraMessageBox.Show("SDE连接参数错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace); } return(null); }
/// <summary> /// 开启SDE连接 /// </summary> private void OpenSDE(ConnectionJSObject connObj) { try { if (pWorkspace == null) { if (connObj != null) { //设置连接属性! IPropertySet pPropertySet = new PropertySetClass(); pPropertySet.SetProperty("SERVER", connObj.server); pPropertySet.SetProperty("INSTANCE", connObj.instance); pPropertySet.SetProperty("DATABASE", connObj.database); pPropertySet.SetProperty("USER", connObj.user); pPropertySet.SetProperty("PASSWORD", connObj.password); pPropertySet.SetProperty("VERSION", connObj.version); //打开SDE空间数据库! pWorkspaceFactory = new SdeWorkspaceFactoryClass(); pWorkspace = pWorkspaceFactory.Open(pPropertySet, 0); if (pWorkspace != null) { pWorkspace2 = (IWorkspace2)pWorkspace; pFeatureWorkspace = (IFeatureWorkspace)pWorkspace; pWorkspaceEdit = (IWorkspaceEdit)pWorkspace; } else { pWorkspace = null; pWorkspace2 = null; pFeatureWorkspace = null; pWorkspaceEdit = null; } } else { throw new Exception(string.Format("输入的连接对象不正确!")); } } } catch (Exception ex) { CloseSDE(); throw new Exception(string.Format("打开SDE空间数据库失败:{0}!", ex.Message)); } }
public IFeatureLayer OracleQueryLayer() { // 创建SqlWorkspaceFactory的对象 Type pFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory"); IWorkspaceFactory pWorkspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(pFactoryType); // 构造连接数据库的参数 IPropertySet pConnectionProps = new PropertySetClass(); pConnectionProps.SetProperty("dbclient", "Oracle11g"); pConnectionProps.SetProperty("serverinstance", "esri"); pConnectionProps.SetProperty("authentication_mode", "DBMS"); pConnectionProps.SetProperty("user", "scott"); pConnectionProps.SetProperty("password", "arcgis"); // 打开工作空间 IWorkspace workspace = pWorkspaceFactory.Open(pConnectionProps, 0); ISqlWorkspace pSQLWorkspace = workspace as ISqlWorkspace; //获取数据库中的所有表的名称 //IStringArray pStringArray= pSQLWorkspace.GetTables(); //for (int i = 0; i < pStringArray.Count; i++) //{ // MessageBox.Show(pStringArray.get_Element(i)); //} // 构造过滤条件 SELECT * FROM PointQueryLayer IQueryDescription queryDescription = pSQLWorkspace.GetQueryDescription("SELECT * FROM TEST"); ITable pTable = pSQLWorkspace.OpenQueryClass("QueryLayerTest", queryDescription); IFeatureLayer pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.FeatureClass = pTable as IFeatureClass; return(pFeatureLayer); }
private IWorkspace OpenWorkspace() { IPropertySet connectionProperties = new PropertySetClass(); int propertyCount = _connectionProps.Length; for (var propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++) { connectionProperties.SetProperty( _connectionProps[propertyIndex], _connectionValues[propertyIndex]); } IWorkspaceFactory wsFactory = WorkspaceUtils.GetWorkspaceFactory(_workspaceFactoryProgId); IWorkspace workspace = wsFactory.Open(connectionProperties, 0); return(workspace); }
} // Main public static IWorkspace ConnectToGDB(String server, String dbclient, String instance, String user, String password, String database, String version) { // how to use this: //connect to GDB //IWorkspace myWorkspace = ConnectToGDB("server1", "Oracle", "sde:oracle:server1/orcl", "map", "map", "none", "SDE.DEFAULTS"); IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("SERVER", server); //propertySet.SetProperty("INSTANCE", instance); //Use this one for old connection syntax propertySet.SetProperty("DBCLIENT", dbclient); //new connection syntax propertySet.SetProperty("DB_CONNECTION_PROPERTIES", instance); //new connection syntax propertySet.SetProperty("DATABASE", database); propertySet.SetProperty("USER", user); propertySet.SetProperty("PASSWORD", password); propertySet.SetProperty("VERSION", version); Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); //Use this one for geodatabase //Type factoryType2 = Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory"); //Use this one for database IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); return(workspaceFactory.Open(propertySet, 0)); } // ConnectToGDB
public IWorkspace GetSDEWorkspace(string _pInstance, string _pUser, string _pPassword, string _pVersion) { IPropertySet pPropertySet = new PropertySetClass(); pPropertySet.SetProperty("INSTANCE", _pInstance); pPropertySet.SetProperty("USER", _pUser); pPropertySet.SetProperty("PASSWORD", _pPassword); pPropertySet.SetProperty("VERSION", _pVersion); IWorkspace pWks = null; IWorkspaceFactory workspaceFactory = (IWorkspaceFactory) new SdeWorkspaceFactoryClass(); try { pWks = workspaceFactory.Open(pPropertySet, 0); } catch (Exception ex) { MessageBox.Show(ex.StackTrace); } return(pWks); }
private void TestConnectSDEData(string server, string instance,string database, string user, string password, string version) { try { m_pWorkspaceFactory = new SdeWorkspaceFactoryClass(); m_pPropSet = new PropertySetClass(); //���ãӣģ�����������Ϣ m_pPropSet.SetProperty("SERVER", server); m_pPropSet.SetProperty("INSTANCE", instance); m_pPropSet.SetProperty("Database", database); m_pPropSet.SetProperty("User", user); m_pPropSet.SetProperty("password", password); m_pPropSet.SetProperty("version", version); m_pWorkspace = m_pWorkspaceFactory.Open(m_pPropSet, 0); m_pFeatureWorkspace = m_pWorkspace as IFeatureWorkspace; ///////////////////////////////////////////////////////// IEnumDatasetName pEnumDSName = m_pWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); IDatasetName pSDEDsName = pEnumDSName.Next(); treeView1.Nodes.Clear(); TreeNode node1; while (pSDEDsName != null) { node1=treeView1.Nodes.Add(pSDEDsName.Name); LoadAllFeatClass(node1,pSDEDsName.Name ); pSDEDsName = pEnumDSName.Next(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void BtConnectarClick(object sender, EventArgs e) { Ws = null; _pPropSet = new PropertySetClass(); _pSdeFact = new SdeWorkspaceFactory(); _pPropSet.SetProperty("SERVER", txtBoxServer.Text); _pPropSet.SetProperty("INSTANCE", txtBoxInstancia.Text); _pPropSet.SetProperty("DATABASE", txtBoxDB.Text); _pPropSet.SetProperty("USER", txtBoxUsuario.Text); _pPropSet.SetProperty("PASSWORD", txtBoxPassword.Text); _pPropSet.SetProperty("VERSION", txtBoxVersao.Text); try { System.Windows.Forms.Cursor.Current = Cursors.WaitCursor; Ws = _pSdeFact.Open(_pPropSet, 0); treeViewDatasets.Nodes.Clear(); treeViewTables.Nodes.Clear(); if (chkBoxFeatueClass.Checked) _enumDsFeatureClasses = Ws.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); if (chkBoxTables.Checked) _enumDsTables = Ws.get_DatasetNames(esriDatasetType.esriDTTable); if (_enumDsFeatureClasses != null) { IDatasetName dsName = _enumDsFeatureClasses.Next(); while (dsName != null) { treeViewDatasets.Nodes.Add(dsName.Name); dsName = _enumDsFeatureClasses.Next(); } } if (_enumDsTables != null) { IDatasetName dsName = _enumDsTables.Next(); while (dsName != null) { treeViewTables.Nodes.Add(dsName.Name); dsName = _enumDsTables.Next(); } } btGerar.Enabled = true; } catch (Exception ex) { MessageBox.Show("Impossible to Connect" + "\n\n" + ex.Message); } finally { System.Windows.Forms.Cursor.Current = Cursors.Default; } }