Esempio n. 1
0
        private static MruConnections LoadFromTextReader(TextReader reader)
        {
            XmlSchema         xmlSchema         = MruConnections.Schema;
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();

            xmlReaderSettings.Schemas.Add(xmlSchema);
            xmlReaderSettings.ValidationType = ValidationType.Schema;
            using (XmlReader xmlReader = XmlReader.Create(reader, xmlReaderSettings)) {
                XmlSerializer  serializer     = new XmlSerializer(typeof(MruConnections), xmlSchema.TargetNamespace);
                object         o              = serializer.Deserialize(xmlReader);
                MruConnections mruConnections = (MruConnections)o;

                return(mruConnections);
            }
        }
        public void SetMruDbConnectionParameterTest1()
        {
            MruConnections        mruConnections = new MruConnections();
            DbConnectionParameter parameter      = new DbConnectionParameter();

            parameter.Provider          = ProviderType.MsSql;
            parameter.DatasourceAddress = "server";
            parameter.Catalog           = "catalog";
            parameter.UserId            = "test";
            parameter.Password          = "******";

            MruDbConnectionParameterAdapter.SetMruDbConnectionParameter(mruConnections, parameter);

            DbConnectionParameter mruParameter = MruDbConnectionParameterAdapter.GetMruDbConnectionParameter(mruConnections);

            Assert.AreEqual(parameter, mruParameter);
        }
        private static void AddAuthentications(Datasource datasource, MruConnections mruConnections, DbConnectionParameter mruParameter)
        {
            AuthenticationList authentications = new AuthenticationList();
            Authentication     authentication  = new Authentication()
            {
                Integrated = mruParameter.IntegratedSecurity,
                UserId     = mruParameter.UserId,
                Password   = mruParameter.Password
            };

            if (authentication.IsUsable)
            {
                authentications.Add(authentication);
            }
            AddAuthenticationsFromMruList(authentications, mruConnections, mruParameter.Provider, datasource);
            datasource.Authentications = authentications;
        }
Esempio n. 4
0
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            try {
                DbConnectionViewModel        vm      = (DbConnectionViewModel)this.LoginControl.DataContext;
                DbConnectionViewModelAdapter vmSaver = new DbConnectionViewModelAdapter();
                MruConnections mruConnections        = vmSaver.Save(vm);

                using (Stream stream = new FileStream("MruConnections2.xml", FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.SequentialScan)) {
                    XmlSchema     xmlSchema  = MruConnections.Schema;
                    XmlSerializer serializer = new XmlSerializer(typeof(MruConnections), xmlSchema.TargetNamespace);
                    serializer.Serialize(stream, mruConnections);
                    stream.Close();
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Get the moust recently used connection parameter stored in the murConnection list.
        /// </summary>
        /// <param name="mruConnections">The most recently used database connection.</param>
        /// <returns>The first entry in the list.</returns>
        public static DbConnectionParameter GetMruDbConnectionParameter(MruConnections mruConnections)
        {
            DbConnectionParameter result = new DbConnectionParameter();

            if (mruConnections.Datasources == null || mruConnections.Datasources.Count == 0)
            {
                return(result);
            }

            Datasources mruDatasources = mruConnections.Datasources[0];

            result.Provider = mruDatasources.Provider;

            if (mruDatasources.Datasource == null || mruDatasources.Datasource.Count == 0)
            {
                return(result);
            }

            Datasource mruDatasource = mruDatasources.Datasource[0];

            result.DatasourceAddress = mruDatasource.Address;
            result.DatasourcePort    = mruDatasource.Port;
            result.DatasourceComment = mruDatasource.Comment;

            if (mruDatasource.Catalogs != null && mruDatasource.Catalogs.Count != 0)
            {
                string mruCatalog = mruDatasource.Catalogs[0];
                result.Catalog = mruCatalog;
            }

            if (mruDatasource.Authentications != null && mruDatasource.Authentications.Count != 0)
            {
                Authentication mruAuthentication = mruDatasource.Authentications[0];
                result.IntegratedSecurity = mruAuthentication.Integrated;
                result.UserId             = mruAuthentication.UserId;
                result.Password           = mruAuthentication.Password;
            }

            return(result);
        }
        /// <summary>
        /// Update the most recently used connection store with the latest parameters used.
        /// </summary>
        /// <param name="mruConnections">The most recently used connection store.</param>
        /// <param name="mruParameter">The most recently connection parameter used.</param>
        /// <returns><c>true</c> if the store was update. <c>false</c> if the store is up to date.</returns>
        public static bool SetMruDbConnectionParameter(MruConnections mruConnections, DbConnectionParameter mruParameter)
        {
            if (mruParameter == null)
            {
                return(false);
            }

            DbConnectionParameter currentMruParameter = MruDbConnectionParameterAdapter.GetMruDbConnectionParameter(mruConnections);

            if (Object.Equals(currentMruParameter, mruParameter))
            {
                return(false);
            }

            DatasourcesList newMruDatasources = new DatasourcesList();

            if (mruParameter.Provider != ProviderType.Undefined)
            {
                Datasources newFirstDatasources = new Datasources()
                {
                    Provider = mruParameter.Provider
                };
                ProviderDataSources datasources = new ProviderDataSources(mruParameter.Provider);
                if (!String.IsNullOrEmpty(mruParameter.DatasourceAddress))
                {
                    Datasource newFirstDatasource = new Datasource()
                    {
                        Address = mruParameter.DatasourceAddress,
                        Comment = mruParameter.DatasourceComment
                    };
                    datasources.Add(newFirstDatasource);

                    AddCatalogs(newFirstDatasource, mruConnections, mruParameter);
                    AddAuthentications(newFirstDatasource, mruConnections, mruParameter);
                    AddDatasourcesFromMruList(datasources, mruConnections);
                }
                newFirstDatasources.Datasource = datasources;

                newMruDatasources.Add(newFirstDatasources);
            }

            // Add all previously used datasources
            IEnumerable <Datasources> otherDatasources = mruConnections.Datasources;

            foreach (Datasources mruDatasources in otherDatasources)
            {
                if (newMruDatasources.Count >= MaxMruEntries)
                {
                    break;
                }

                if (newMruDatasources.Contains(mruDatasources))
                {
                    continue;
                }

                newMruDatasources.Add(mruDatasources);
            }

            mruConnections.Datasources = newMruDatasources;

            return(true);
        }
        private static void AddDatasourcesFromMruList(ProviderDataSources datasources, MruConnections mruConnections)
        {
            ProviderType providerId = datasources.ProviderType;
            Datasources  provider   = mruConnections.FindDatasources(providerId);

            if (provider == null)
            {
                return;
            }

            if (provider.Datasource == null || provider.Datasource.Count == 0)
            {
                return;
            }

            foreach (Datasource datasource in provider.Datasource)
            {
                if (!ContainsDatasource(datasources, datasource.Address))
                {
                    datasources.Add(datasource);
                }
            }
        }
        private static void AddAuthenticationsFromMruList(AuthenticationList authentications, MruConnections mruConnections, ProviderType providerId, Datasource datasource)
        {
            Datasources providerDatasources = mruConnections.FindDatasources(providerId);

            if (providerDatasources == null)
            {
                return;
            }

            if (providerDatasources.Datasource == null || providerDatasources.Datasource.Count == 0)
            {
                return;
            }

            Datasource mruDatasource = providerDatasources.FindDatasourceByAddress(datasource.Address);

            if (mruDatasource == null || mruDatasource.Authentications == null)
            {
                return;
            }

            foreach (var authentication in mruDatasource.Authentications)
            {
                if (authentications.Count >= MaxMruEntries)
                {
                    break;
                }
                if (!authentications.Contains(authentication))
                {
                    authentications.Add(authentication);
                }
            }
        }