Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Windows Forms

UDL.Net, A ConnectionString Editor Built from Scratch

Rate me:
Please Sign up or sign in to vote.
4.80/5 (23 votes)
24 Feb 2010CPOL5 min read 50.1K   2.8K   54  
UDL.Net is a ConnectionString editor built from scratch, modeled after the Universal Data Link (UDL) and the Visual Studio DataConnectionDialog.
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace UDL
{
    internal class DbProvidersCollection
    {
        Dictionary<string, DbProvider> _providers;
        public DbProvidersCollection()
        {
            _providers = new Dictionary<string, DbProvider>();
            DataTable dataProvidersTable = DbProviderFactories.GetFactoryClasses();
            foreach (DataRow row in dataProvidersTable.Rows)
            {
                DbProvider provider = new DbProvider(row);
                if(!_providers.ContainsKey(provider.InvariantName))
                    _providers.Add(provider.InvariantName, provider);
            }
        }

        public DbProvider this[string invariantName]
        {
            get
            {
                if (_providers.ContainsKey(invariantName))
                    return _providers[invariantName];
                return null;
            }
        }

        public void Add(DbProvider provider)
        {
            if (!_providers.ContainsKey(provider.InvariantName))
                _providers.Add(provider.InvariantName, provider);
        }

        public void Clear()
        {
            _providers.Clear();
        }

        /// <summary>
        /// Returns an array of filtered DataProviders
        /// </summary>
        public DbProvider[] AvailableProviders
        {
            get
            {
                bool isFiltered = ConnectionStringDialog.Filters.Count > 0;
                List<DbProvider> result = new List<DbProvider>();
                foreach (DbProvider provider in _providers.Values)
                {
                    if (isFiltered)
                    {
                        if (ConnectionStringDialog.FilterMode == FilterMode.Exclude)
                        {
                            if (!ConnectionStringDialog.Filters.Contains(provider.InvariantName))
                                result.Add(provider);
                        }
                        else
                        {
                            if (ConnectionStringDialog.Filters.Contains(provider.InvariantName))
                                result.Add(provider);
                        }
                    }
                    else
                        result.Add(provider);
                }
                return result.ToArray();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions