Beispiel #1
0
        public static void Save()
        {
            //temporarily empty all passwords that don't need to be saved
            Dictionary <DatabaseConnectionSettings, string> tmp        = new Dictionary <DatabaseConnectionSettings, string> ();
            DatabaseConnectionSettingsCollection            collection = new DatabaseConnectionSettingsCollection();

            foreach (DatabaseConnectionContext context in contexts)
            {
                if (!context.ConnectionSettings.SavePassword)
                {
                    tmp.Add(context.ConnectionSettings, context.ConnectionSettings.Password);
                    context.ConnectionSettings.Password = null;
                }
                collection.Add(context.ConnectionSettings);
            }

            using (FileStream fs = new FileStream(configFile, FileMode.Create)) {
                XmlSerializer serializer = new XmlSerializer(typeof(DatabaseConnectionSettingsCollection));
                serializer.Serialize(fs, collection);
            }

            foreach (KeyValuePair <DatabaseConnectionSettings, string> pair in tmp)
            {
                pair.Key.Password = pair.Value;
            }
        }
Beispiel #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);
                }
            }
        }