使用FDO API连接到各种数据源

Map 3D可以通过FDO技术连接到各种数据源,包括SHP文件,SDF文件,MySQL,SQL Server Spatial,Oracle等等。当然了,连接到不同数据源肯定有不同的参数,或者理解成连接字符串的组成部分。那这些参数都是什么呢?我们可以通过下面一段代码来深入理解这个参数:

闲话不多少,直接上代码,你可以在Map 3D中运行一下就会发觉他的奥妙:


using
Autodesk.AutoCAD; using Autodesk.AutoCAD.Runtime; using OSGeo.FDO; using OSGeo.FDO.Commands; using OSGeo.FDO.Commands.Schema; using OSGeo.FDO.Connections; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.ApplicationServices; using OSGeo.FDO.Schema; using OSGeo.FDO.Connections.Capabilities; using OSGeo.FDO.ClientServices; using OSGeo.FDO.Commands.DataStore; public class Class1 { IConnection m_pConnection = null; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; FeatureClass m_baseClass; FeatureClass m_riverClass; ProviderCollection m_Providers; string m_sProviderName; [CommandMethod("connect")] public void Connect() { IConnectionManager connMgr; int index; Provider provider; IProviderRegistry registry = FeatureAccessManager.GetProviderRegistry(); m_Providers = registry.GetProviders(); for (int i = 0; i < m_Providers.Count; i++) { provider = m_Providers[i]; ed.WriteMessage(string.Format("FDO support provider {0}, its index is {1} \n", provider.Name, i)); } PromptIntegerResult intRes = ed.GetInteger("please input the provider's index"); if (intRes.Status == PromptStatus.OK) { index = intRes.Value; provider = m_Providers[index]; m_sProviderName = provider.Name; string shortName = provider.Name.Split('.')[1]; try { connMgr = FeatureAccessManager.GetConnectionManager(); m_pConnection = connMgr.CreateConnection(m_sProviderName); IConnectionInfo connInfo = m_pConnection.ConnectionInfo; IConnectionPropertyDictionary properties = connInfo.ConnectionProperties; InputParametersValue(properties); ConnectionState connState = m_pConnection.Open(); ed.WriteMessage("connect status is "+connState.ToString() + "\n"); } catch (OSGeo.FDO.Common.Exception exception) { ed.WriteMessage("There are some exceptions with message : " + exception.Message + "\n"); } } else { ed.WriteMessage("you did not select a correct provider , exit \n"); return; } } [CommandMethod("DisConnect")] public void CloseConnection() { m_pConnection.Close(); } private void InputParametersValue(IConnectionPropertyDictionary properties) { string[] propertiesNames = properties.PropertyNames; foreach (string name in propertiesNames) { PromptStringOptions pso = new PromptStringOptions("Please input the value for \"" + name + "\":"); PromptResult psr = ed.GetString(pso); if (properties.IsPropertyRequired(name)) { while (psr.Status != PromptStatus.OK) { ed.WriteMessage(string.Format("Parameter \"{0}\" is required, please input value again\n", name)); psr = ed.GetString(pso); } properties.SetProperty(name, psr.StringResult); } } } private void ListPropertiesParameters(IDataStorePropertyDictionary properties) { foreach (string name in properties.PropertyNames) { ed.WriteMessage(name + "\n"); } } [CommandMethod("CreateDataStore")] public void CreateDataStore() { ICreateDataStore createDS = m_pConnection.CreateCommand(CommandType.CommandType_CreateDataStore) as ICreateDataStore; IDataStorePropertyDictionary properties = createDS.DataStoreProperties; InputParametersValue(properties); createDS.Execute(); } }

Related Posts Plugin for WordPress, Blogger...