Connect to data source using FDO API

By Daniel Du

Here is a code snippet to demonstrate how to connect to data sources, with this tool code snippet, you will get deep understanding which parameters are needed for each provider.

using Autodesk.AutoCAD;    using Autodesk.AutoCAD.ApplicationServices;    using Autodesk.AutoCAD.EditorInput;    using Autodesk.AutoCAD.Runtime;    using OSGeo.FDO;    using OSGeo.FDO.ClientServices;    using OSGeo.FDO.Commands;    using OSGeo.FDO.Commands.DataStore;    using OSGeo.FDO.Commands.Schema;    using OSGeo.FDO.Connections;    using OSGeo.FDO.Connections.Capabilities;    using OSGeo.FDO.Schema;         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 againn", 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();        }    }

Comments

2 responses to “Connect to data source using FDO API”

  1. Firexoulz Avatar
    Firexoulz

    Thank you very much this is a great help… Nice blog.. Keep em’ up!… Thank you for sharing your wisdom to others…

  2. Thank you for the comment, I am glad to hear it helps :)

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading