示例#1
0
        /// <summary>
        /// Refreshes the connection.
        /// </summary>
        /// <param name="name">The name of the connection.</param>
        public void RefreshConnection(string name)
        {
            if (NameExists(name))
            {
                FdoConnection conn = this.GetConnection(name);
                conn.Close();

                //HACK: FDO #660 workaround
                if (conn.Provider.ToUpper().StartsWith("OSGEO.POSTGRESQL"))
                {
                    conn.ConnectionString = conn.ConnectionString;
                }

                conn.Open();

                /*
                 * //TODO: I got a bad feeling that this may break something
                 * //which may rely on the old connection. Verify this.
                 * FdoConnection oldConn = this.GetConnection(name);
                 * string provider = oldConn.Provider;
                 * string connStr = oldConn.ConnectionString;
                 * oldConn.Close();
                 * oldConn.Dispose();
                 *
                 * FdoConnection newConn = new FdoConnection(provider, connStr);
                 * newConn.Open();
                 * _ConnectionDict[name] = newConn;
                 */
                ConnectionRefreshed(this, new EventArgs <string>(name));
            }
        }
示例#2
0
        /// <summary>
        /// Removes all connections
        /// </summary>
        public void Clear()
        {
            List <string> names = new List <string>(GetConnectionNames());

            foreach (string name in names)
            {
                //Yes we're inlining RemoveConnection(), but we want any cancel action to cancel
                //the Clear() method
                if (_ConnectionDict.ContainsKey(name))
                {
                    ConnectionBeforeRemoveEventArgs e = new ConnectionBeforeRemoveEventArgs(name);
                    this.BeforeConnectionRemove(this, e);
                    if (e.Cancel)
                    {
                        return;
                    }

                    FdoConnection conn = _ConnectionDict[name];
                    conn.Close();
                    _ConnectionDict.Remove(name);
                    conn.Dispose();
                    if (this.ConnectionRemoved != null)
                    {
                        this.ConnectionRemoved(this, new EventArgs <string>(name));
                    }
                }
            }
        }
        public void TestConnection()
        {
            FdoProviderInfo provider = _view.SelectedProvider;
            string          connStr  = ExpressUtility.ConvertFromNameValueCollection(_view.ConnectProperties);

            FdoConnection conn = new FdoConnection(provider.Name, connStr);

            try
            {
                FdoConnectionState state = conn.Open();
                if (state == FdoConnectionState.Open || state == FdoConnectionState.Pending)
                {
                    _view.ShowMessage(null, "Test successful");
                    conn.Close();
                }
                else
                {
                    _view.ShowError("Connection test failed");
                }
            }
            catch (FdoException ex)
            {
                _view.ShowError(ex.InnerException.Message);
            }
            finally
            {
                conn.Dispose();
            }
        }
示例#4
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _outConn.Close();
         _outConn.Dispose();
     }
     base.Dispose(disposing);
 }
 internal bool Test()
 {
     using (var conn = new FdoConnection(_view.Provider, GetBaseConnectionString()))
     {
         if (conn.Open() != FdoConnectionState.Closed)
         {
             conn.Close();
             return(true);
         }
     }
     return(false);
 }
        public void PendingConnect()
        {
            InitConnection();
            try
            {
                if (_conn.State != FdoConnectionState.Closed)
                {
                    _conn.Close();
                }

                _conn.ConnectionString = string.Format("{0}={1};{2}={3};{4}={5}", _view.ServiceParameter, _view.Service, _view.UsernameParameter, _view.Username, _view.PasswordParameter, _view.Password);
                if (_conn.Open() == FdoConnectionState.Pending)
                {
                    List <DataStoreInfo> datastores = new List <DataStoreInfo>();
                    var cmds = _conn.Capability.GetArrayCapability(CapabilityType.FdoCapabilityType_CommandList);
                    if (Array.IndexOf(cmds, CommandType.CommandType_ListDataStores) >= 0)
                    {
                        using (var svc = _conn.CreateFeatureService())
                        {
                            var stores = svc.ListDataStores(false);
                            foreach (var store in stores)
                            {
                                var ds = new DataStoreInfo(
                                    store.Name,
                                    store.Name + ((store.IsFdoEnabled) ? "" : " (*)"),
                                    store.IsFdoEnabled);

                                datastores.Add(ds);
                            }
                        }
                    }
                    else
                    {
                        var prop = _conn.GetConnectTimeProperty(_view.DataStoreParameter) as EnumerableDictionaryProperty;
                        if (prop != null)
                        {
                            foreach (string name in prop.Values)
                            {
                                datastores.Add(new DataStoreInfo(name, name, true));
                            }
                        }
                    }
                    SetDataStore(datastores.ToArray());
                }
            }
            catch (Exception ex)
            {
                _view.ShowError(ex.Message);
            }
        }
        public override int Execute()
        {
            CommandStatus retCode;
            FdoConnection conn = null;

            try
            {
                conn = new FdoConnection(_provider, _connStr);
                conn.Open();
            }
            catch (OSGeo.FDO.Common.Exception ex)
            {
                WriteException(ex);
                retCode = CommandStatus.E_FAIL_CONNECT;
                return((int)retCode);
            }

            using (conn)
            {
                using (FdoFeatureService service = conn.CreateFeatureService())
                {
                    try
                    {
                        service.CreateDataStore(_dstoreStr);
                        WriteLine("Data Store Created!");
                        retCode = CommandStatus.E_OK;
                    }
                    catch (OSGeo.FDO.Common.Exception ex)
                    {
                        WriteException(ex);
                        retCode = CommandStatus.E_FAIL_CREATE_DATASTORE;
                        return((int)retCode);
                    }
                }
                if (conn.State != FdoConnectionState.Closed)
                {
                    conn.Close();
                }
            }
            return((int)retCode);
        }
示例#8
0
        /// <summary>
        /// Removes the connection.
        /// </summary>
        /// <param name="name">The name of the connection.</param>
        public void RemoveConnection(string name)
        {
            if (_ConnectionDict.ContainsKey(name))
            {
                ConnectionBeforeRemoveEventArgs e = new ConnectionBeforeRemoveEventArgs(name);
                this.BeforeConnectionRemove(this, e);
                if (e.Cancel)
                {
                    return;
                }

                FdoConnection conn = _ConnectionDict[name];
                conn.Close();
                _ConnectionDict.Remove(name);
                conn.Dispose();
                if (this.ConnectionRemoved != null)
                {
                    this.ConnectionRemoved(this, new EventArgs <string>(name));
                }
            }
        }
        public void PendingConnect()
        {
            try
            {
                if (_conn.State != FdoConnectionState.Closed)
                {
                    _conn.Close();
                }

                _conn.ConnectionString = string.Format("Server={0};Instance={1};Username={2};Password={3}", _view.Server, _view.Instance, _view.Username, _view.Password);
                if (_conn.Open() == FdoConnectionState.Pending)
                {
                    EnumerableDictionaryProperty prop = (EnumerableDictionaryProperty)_conn.GetConnectTimeProperty("DataStore");
                    SetDataStore(prop.Values);
                }
            }
            catch (Exception ex)
            {
                _view.ShowError(ex.Message);
            }
        }
示例#10
0
        public void TestConnection()
        {
            FdoConnection conn = new FdoConnection("OSGeo.OGR", _view.BuilderObject.ToConnectionString());

            try
            {
                FdoConnectionState state = conn.Open();
                if (state == FdoConnectionState.Open)
                {
                    _view.ShowMessage(null, "Test successful");
                    conn.Close();
                }
                else
                {
                    _view.ShowError("Connection test failed");
                }
            }
            catch (FdoException ex)
            {
                _view.ShowError(ex.InnerException.Message);
            }
        }
示例#11
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            var schema = _schema;

            //Remove elements that have been unchecked.
            foreach (TreeNode clsNode in treeSchema.Nodes)
            {
                string className = clsNode.Name;
                int    index     = schema.Classes.IndexOf(className);
                if (!clsNode.Checked)
                {
                    if (index >= 0)
                    {
                        schema.Classes.RemoveAt(index);
                    }
                }
                else
                {
                    if (index >= 0)
                    {
                        ClassDefinition clsDef = schema.Classes[index];
                        foreach (TreeNode propNode in clsNode.Nodes)
                        {
                            if (!propNode.Checked)
                            {
                                string propName = propNode.Text;
                                int    pidx     = clsDef.Properties.IndexOf(propName);
                                if (pidx >= 0)
                                {
                                    clsDef.Properties.RemoveAt(pidx);
                                    if (clsDef.IdentityProperties.Contains(propName))
                                    {
                                        int idpdx = clsDef.IdentityProperties.IndexOf(propName);
                                        clsDef.IdentityProperties.RemoveAt(idpdx);
                                    }
                                    if (clsDef.ClassType == ClassType.ClassType_FeatureClass)
                                    {
                                        FeatureClass fc = (FeatureClass)clsDef;
                                        if (fc.GeometryProperty.Name == propName)
                                        {
                                            fc.GeometryProperty = null;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (rdXml.Checked)
            {
                using (var ios = new IoFileStream(txtXml.Text, "w"))
                {
                    using (var writer = new XmlWriter(ios, false, XmlWriter.LineFormat.LineFormat_Indent))
                    {
                        schema.WriteXml(writer);
                        writer.Close();
                    }
                    ios.Close();
                }

                MessageService.ShowMessage("Schema saved to: " + txtXml.Text);
                this.DialogResult = DialogResult.OK;
            }
            else if (rdFile.Checked)
            {
                string fileName = txtFile.Text;
                if (ExpressUtility.CreateFlatFileDataSource(fileName))
                {
                    FdoConnection conn        = ExpressUtility.CreateFlatFileConnection(fileName);
                    bool          disposeConn = true;
                    using (FdoFeatureService svc = conn.CreateFeatureService())
                    {
                        svc.ApplySchema(schema);
                        if (MessageService.AskQuestion("Schema saved to: " + txtFile.Text + " connect to it?", "Saved"))
                        {
                            FdoConnectionManager mgr = ServiceManager.Instance.GetService <FdoConnectionManager>();
                            string name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), "");
                            if (name == null)
                            {
                                return;
                            }

                            while (name == string.Empty || mgr.NameExists(name))
                            {
                                MessageService.ShowError(ResourceService.GetString("ERR_CONNECTION_NAME_EMPTY_OR_EXISTS"));
                                name = MessageService.ShowInputBox(ResourceService.GetString("TITLE_CONNECTION_NAME"), ResourceService.GetString("PROMPT_ENTER_CONNECTION"), name);

                                if (name == null)
                                {
                                    return;
                                }
                            }
                            disposeConn = false;
                            mgr.AddConnection(name, conn);
                        }
                    }
                    if (disposeConn)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                    this.DialogResult = DialogResult.OK;
                }
            }
        }
        public bool Create()
        {
            bool          ok      = true;
            FdoConnection conn    = new FdoConnection(_view.Provider, GetBaseConnectionString());
            bool          created = false;
            bool          cleanup = true;

            try
            {
                using (var svc = conn.CreateFeatureService())
                {
                    try
                    {
                        CreateDataStore(svc);
                        created = true;
                    }
                    catch (Exception ex)
                    {
                        _view.ShowError(ex);
                        created = false;
                        ok      = false;
                    }
                }

                if (created)
                {
                    //Amend the connection string to include the data store
                    var builder = new DbConnectionStringBuilder();
                    builder.ConnectionString          = conn.ConnectionString;
                    builder[_view.DataStoreParameter] = _view.DataStoreName;
                    conn.ConnectionString             = builder.ConnectionString;
                    conn.Open();

                    using (var svc = conn.CreateFeatureService())
                    {
                        CreateDefaultSpatialContext(svc);
                    }
                }
            }
            finally
            {
                if (created)
                {
                    if (File.Exists(_view.SchemaFile))
                    {
                        ApplySchemas(conn);
                    }

                    if (_view.ConnectOnCreate)
                    {
                        _connMgr.AddConnection(_view.ConnectionName, conn);
                        cleanup = false;
                    }
                }

                if (cleanup)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }

            return(ok);
        }