Exemplo n.º 1
0
        //TODO: show errors
        public static void EnsureConnection(DatabaseConnectionContext context, DatabaseConnectionContextCallback callback, object state)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            IConnectionPool pool = context.ConnectionPool;

            if (pool.IsInitialized)
            {
                callback(context, true, state);
                return;
            }

            IDbFactory fac = DbFactoryService.GetDbFactory(context.ConnectionSettings);
            //FIXME: connection settings dialog
            //bool requiresPassword = fac.GetCapabilities ("ConnectionSettings", SchemaActions.Schema) == (int)ConnectionSettingsCapabilities.Password;
            bool requiresPassword = true;

            if (!context.ConnectionSettings.SavePassword && String.IsNullOrEmpty(context.ConnectionSettings.Password) && requiresPassword)
            {
                string password = MessageService.GetPassword(
                    AddinCatalog.GetString("Please enter the password for connection '{0}'",
                                           context.ConnectionSettings.Name),
                    AddinCatalog.GetString("Enter Password")
                    );

                if (password == null)
                {
                    callback(context, false, state);
                    return;
                }
                else
                {
                    context.ConnectionSettings.Password = password;
                }
            }

            EnsureConnectionState internalState = new EnsureConnectionState(context, callback, state);

            ThreadPool.QueueUserWorkItem(new WaitCallback(EnsureConnectionThreaded), internalState);
        }
Exemplo n.º 2
0
        internal static void Initialize(string configFile)
        {
            DatabaseConnectionSettingsCollection connections = null;

            if (File.Exists(configFile))
            {
                try {
                    using (FileStream fs = File.OpenRead(configFile)) {
                        XmlSerializer serializer = new XmlSerializer(typeof(DatabaseConnectionSettingsCollection));
                        connections = (DatabaseConnectionSettingsCollection)serializer.Deserialize(fs);
                    }
                } catch {
                    LoggingService.LogError(AddinCatalog.GetString("Unable to load stored SQL connection information."));
                    File.Delete(configFile);
                }
            }

            contexts = new DatabaseConnectionContextCollection();
            if (connections != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (DatabaseConnectionSettings settings in connections)
                {
                    IDbFactory fac = DbFactoryService.GetDbFactory(settings);
                    if (fac == null)
                    {
                        sb.Append("Error: unable to load database provider '");
                        sb.Append(settings.ProviderIdentifier);
                        sb.Append("' for connection '");
                        sb.Append(settings.Name);
                        sb.Append("'");
                        sb.Append(Environment.NewLine);
                        continue;
                    }

                    contexts.Add(new DatabaseConnectionContext(settings));
                }

                if (sb.Length > 0)
                {
                    Exception ex = new Exception(sb.ToString());
                    QueryService.RaiseException(ex);
                }
            }
        }