public static string GetFilePath(string connectionString, DatabaseType dbType)
        {
            var helper = RepositoryHelper.CreateEngineHelper(dbType);

            return(helper.PathFromConnectionString(connectionString));
        }
        internal static Dictionary <string, DatabaseInfo> GetDataConnections(SqlCeToolboxPackage package,
                                                                             bool includeServerConnections, bool serverConnectionsOnly)
        {
            // http://www.mztools.com/articles/2007/MZ2007018.aspx
            Dictionary <string, DatabaseInfo> databaseList = new Dictionary <string, DatabaseInfo>();
            var dataExplorerConnectionManager =
                package.GetServiceHelper(typeof(IVsDataExplorerConnectionManager)) as IVsDataExplorerConnectionManager;

            //Test code...

            //var objIVsDataProviderManager = package.GetServiceHelper(typeof(IVsDataProviderManager)) as IVsDataProviderManager;
            //var objIVsDataConnectionManager = package.GetServiceHelper(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;
            //var objIVsDataSourceManager = package.GetServiceHelper(typeof(IVsDataSourceManager)) as IVsDataSourceManager;

            //IVsDataProvider objIVsDataProvider;
            //foreach (var objIVsDataSource in objIVsDataSourceManager.Sources)
            //{
            //    //System.Diagnostics.Debug.WriteLine(objIVsDataSource.DisplayName);
            //    //System.Diagnostics.Debug.WriteLine(objIVsDataSource.Guid.ToString());

            //    //foreach (var objProviderGuid in objIVsDataSource.GetProviders())
            //    //{
            //    //    objIVsDataProvider = objIVsDataProviderManager.GetDataProvider(objProviderGuid);
            //    //    System.Diagnostics.Debug.WriteLine(objIVsDataProvider.DisplayName);
            //    //    System.Diagnostics.Debug.WriteLine(objIVsDataProvider.Guid.ToString());
            //    //}
            //}

            // End test code

            Guid provider35            = new Guid(Resources.SqlCompact35Provider);
            Guid provider40            = new Guid(Resources.SqlCompact40Provider);
            Guid provider40Private     = new Guid(Resources.SqlCompact40PrivateProvider);
            Guid providerSqLite        = new Guid(Resources.SQLiteProvider);
            Guid providerSqlitePrivate = new Guid(Resources.SqlitePrivateProvider);

            bool isV35Installed = RepositoryHelper.IsV35Installed() && DdexProviderIsInstalled(provider35);
            bool isV40Installed = RepositoryHelper.IsV40Installed() &&
                                  (DdexProviderIsInstalled(provider40) || DdexProviderIsInstalled(provider40Private));

            if (dataExplorerConnectionManager != null)
            {
                foreach (var connection in dataExplorerConnectionManager.Connections.Values)
                {
                    try
                    {
                        var objProviderGuid = connection.Provider;
                        if (!serverConnectionsOnly)
                        {
                            if ((objProviderGuid == provider35 && isV35Installed) ||
                                (objProviderGuid == provider40 && isV40Installed) ||
                                (objProviderGuid == provider40Private && isV40Installed))
                            {
                                DatabaseType dbType = DatabaseType.SQLCE40;
                                if (objProviderGuid == provider35)
                                {
                                    dbType = DatabaseType.SQLCE35;
                                }
                                var serverVersion = "4.0";
                                if (dbType == DatabaseType.SQLCE35)
                                {
                                    serverVersion = "3.5";
                                }

                                var sConnectionString =
                                    DataProtection.DecryptString(connection.EncryptedConnectionString);
                                if (!sConnectionString.Contains("Mobile Device"))
                                {
                                    DatabaseInfo info = new DatabaseInfo()
                                    {
                                        Caption            = connection.DisplayName,
                                        FromServerExplorer = true,
                                        DatabaseType       = dbType,
                                        ServerVersion      = serverVersion,
                                        ConnectionString   = sConnectionString
                                    };
                                    info.FileIsMissing = RepositoryHelper.IsMissing(info);
                                    if (!databaseList.ContainsKey(sConnectionString))
                                    {
                                        databaseList.Add(sConnectionString, info);
                                    }
                                }
                            }

                            if (objProviderGuid == providerSqLite ||
                                objProviderGuid == providerSqlitePrivate)
                            {
                                DatabaseType dbType = DatabaseType.SQLite;

                                var sConnectionString =
                                    DataProtection.DecryptString(connection.EncryptedConnectionString);
                                DatabaseInfo info = new DatabaseInfo()
                                {
                                    Caption            = connection.DisplayName,
                                    FromServerExplorer = true,
                                    DatabaseType       = dbType,
                                    ServerVersion      = SqliteEngineVersion,
                                    ConnectionString   = sConnectionString
                                };
                                info.FileIsMissing = RepositoryHelper.IsMissing(info);
                                if (!databaseList.ContainsKey(sConnectionString))
                                {
                                    databaseList.Add(sConnectionString, info);
                                }
                            }
                        }
                        if (includeServerConnections && objProviderGuid == new Guid(Resources.SqlServerDotNetProvider))
                        {
                            var sConnectionString = DataProtection.DecryptString(connection.EncryptedConnectionString);
                            var info = new DatabaseInfo()
                            {
                                Caption            = connection.DisplayName,
                                FromServerExplorer = true,
                                DatabaseType       = DatabaseType.SQLServer,
                                ServerVersion      = string.Empty,
                                ConnectionString   = sConnectionString
                            };
                            if (!databaseList.ContainsKey(sConnectionString))
                            {
                                databaseList.Add(sConnectionString, info);
                            }
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
#if SSMS
            try
            {
                if (package.TelemetryVersion().Major == 140)
                {
                    var objectExplorerManager = new ObjectExplorerManager(package);
                    var list = objectExplorerManager.GetAllServerUserDatabases();
                    foreach (var item in list)
                    {
                        if (!databaseList.ContainsKey(item.Key))
                        {
                            databaseList.Add(item.Key, item.Value);
                        }
                    }
                }
            }
            catch (MissingMethodException)
            {
            }
#endif
            return(databaseList);
        }
        internal static Dictionary <string, DatabaseInfo> GetDataConnections(EFCorePowerToolsPackage package)
        {
            // http://www.mztools.com/articles/2007/MZ2007018.aspx
            Dictionary <string, DatabaseInfo> databaseList = new Dictionary <string, DatabaseInfo>();
            var  dataExplorerConnectionManager             = package.GetService <IVsDataExplorerConnectionManager>();
            Guid provider40            = new Guid(EFCorePowerTools.Shared.Resources.SqlCompact40Provider);
            Guid provider40Private     = new Guid(EFCorePowerTools.Shared.Resources.SqlCompact40PrivateProvider);
            Guid providerSqLite        = new Guid(EFCorePowerTools.Shared.Resources.SQLiteProvider);
            Guid providerSqlitePrivate = new Guid(EFCorePowerTools.Shared.Resources.SQLitePrivateProvider);
            Guid providerNpgsql        = new Guid(Resources.NpgsqlProvider);
            Guid providerMysql         = new Guid(Resources.MysqlVSProvider);
            Guid providerOracle        = new Guid(Resources.OracleProvider);

            bool isV40Installed = RepositoryHelper.IsV40Installed() &&
                                  (DdexProviderIsInstalled(provider40) || DdexProviderIsInstalled(provider40Private));

            if (dataExplorerConnectionManager != null)
            {
                foreach (var connection in dataExplorerConnectionManager.Connections.Values)
                {
                    try
                    {
                        var sConnectionString = DataProtection.DecryptString(connection.EncryptedConnectionString);
                        var info = new DatabaseInfo()
                        {
                            Caption            = connection.DisplayName,
                            FromServerExplorer = true,
                            DatabaseType       = DatabaseType.SQLCE35,
                            ConnectionString   = sConnectionString
                        };
                        var objProviderGuid = connection.Provider;

                        if ((objProviderGuid == provider40 && isV40Installed ||
                             objProviderGuid == provider40Private && isV40Installed) &&
                            !sConnectionString.Contains("Mobile Device"))
                        {
                            info.DatabaseType = DatabaseType.SQLCE40;
                        }
                        if (objProviderGuid == providerSqLite ||
                            objProviderGuid == providerSqlitePrivate)
                        {
                            info.DatabaseType = DatabaseType.SQLite;
                        }
                        if (objProviderGuid == new Guid(Resources.SqlServerDotNetProvider) ||
                            objProviderGuid == providerNpgsql)
                        {
                            info.DatabaseType = objProviderGuid == providerNpgsql ? DatabaseType.Npgsql : DatabaseType.SQLServer;
                        }

                        // This provider depends on https://dev.mysql.com/downloads/windows/visualstudio/
                        if (objProviderGuid == providerMysql)
                        {
                            info.DatabaseType = DatabaseType.Mysql;
                        }

                        if (info.DatabaseType != DatabaseType.SQLCE35 &&
                            !databaseList.ContainsKey(sConnectionString))
                        {
                            databaseList.Add(sConnectionString, info);
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
            return(databaseList);
        }
        internal void ValidateConnections(SqlCeToolboxPackage package)
        {
            var dataExplorerConnectionManager =
                package.GetServiceHelper(typeof(IVsDataExplorerConnectionManager)) as IVsDataExplorerConnectionManager;
            var removals = new List <IVsDataExplorerConnection>();

            if (dataExplorerConnectionManager != null)
            {
                foreach (var connection in dataExplorerConnectionManager.Connections.Values)
                {
                    try
                    {
                        var objProviderGuid = connection.Provider;
                        if (
                            (objProviderGuid == new Guid(Resources.SqlCompact35Provider) && RepositoryHelper.IsV35Installed()) ||
                            (objProviderGuid == new Guid(Resources.SqlCompact40Provider) && RepositoryHelper.IsV40Installed()) ||
                            (objProviderGuid == new Guid(Resources.SqlCompact40PrivateProvider) && RepositoryHelper.IsV40Installed() ||
                             (objProviderGuid == new Guid(Resources.SqlitePrivateProvider) && IsSqLiteDbProviderInstalled())
                            ))
                        {
                            connection.Connection.Open();
                            connection.Connection.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetType() == typeof(SQLiteException))
                        {
                            removals.Add(connection);
                        }
                        if (ex.GetType().Name == "SqlCeException")
                        {
                            removals.Add(connection);
                        }
                        if (ex.GetType().Name == "SqlCeInvalidDatabaseFormatException")
                        {
                            removals.Add(connection);
                        }
                        if (ex.GetType() == typeof(ArgumentException))
                        {
                            removals.Add(connection);
                        }
                        if (ex.GetType() == typeof(KeyNotFoundException))
                        {
                            removals.Add(connection);
                        }
                        throw;
                    }
                }
                for (int i = removals.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        dataExplorerConnectionManager.RemoveConnection(removals[i]);
                    }
                    catch (ArgumentException)
                    {
                    }
                    catch (IndexOutOfRangeException)
                    {
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                }
            }

            var ownConnections = GetOwnDataConnections();

            foreach (var item in ownConnections)
            {
                try
                {
                    using (Helpers.RepositoryHelper.CreateRepository(item.Value))
                    {
                    }
                }
                catch (Exception ex)
                {
                    if (ex.GetType().Name == "SqlCeException" ||
                        ex.GetType().Name == "SqlCeInvalidDatabaseFormatException")
                    {
                        RemoveDataConnection(item.Value.ConnectionString);
                    }
                    throw;
                }
            }
        }
        internal static Dictionary <string, DatabaseInfo> GetDataConnections(EFCorePowerToolsPackage package,
                                                                             bool includeServerConnections = true)
        {
            // http://www.mztools.com/articles/2007/MZ2007018.aspx
            Dictionary <string, DatabaseInfo> databaseList = new Dictionary <string, DatabaseInfo>();
            var  dataExplorerConnectionManager             = package.GetService <IVsDataExplorerConnectionManager>();
            Guid provider40            = new Guid(Resources.SqlCompact40Provider);
            Guid provider40Private     = new Guid(Resources.SqlCompact40PrivateProvider);
            Guid providerSqLite        = new Guid(Resources.SQLiteProvider);
            Guid providerSqlitePrivate = new Guid(Resources.SqlitePrivateProvider);

            bool isV40Installed = RepositoryHelper.IsV40Installed() &&
                                  (DdexProviderIsInstalled(provider40) || DdexProviderIsInstalled(provider40Private));

            if (dataExplorerConnectionManager != null)
            {
                foreach (var connection in dataExplorerConnectionManager.Connections.Values)
                {
                    try
                    {
                        var objProviderGuid = connection.Provider;
                        if (objProviderGuid == provider40 && isV40Installed ||
                            objProviderGuid == provider40Private && isV40Installed)
                        {
                            DatabaseType dbType        = DatabaseType.SQLCE40;
                            var          serverVersion = "4.0";

                            var sConnectionString =
                                DataProtection.DecryptString(connection.EncryptedConnectionString);
                            if (!sConnectionString.Contains("Mobile Device"))
                            {
                                DatabaseInfo info = new DatabaseInfo()
                                {
                                    Caption            = connection.DisplayName,
                                    FromServerExplorer = true,
                                    DatabaseType       = dbType,
                                    ServerVersion      = serverVersion,
                                    ConnectionString   = sConnectionString
                                };
                                info.FileIsMissing = RepositoryHelper.IsMissing(info);
                                if (!databaseList.ContainsKey(sConnectionString))
                                {
                                    databaseList.Add(sConnectionString, info);
                                }
                            }
                        }

                        if (objProviderGuid == providerSqLite ||
                            objProviderGuid == providerSqlitePrivate)
                        {
                            DatabaseType dbType = DatabaseType.SQLite;

                            var sConnectionString =
                                DataProtection.DecryptString(connection.EncryptedConnectionString);
                            DatabaseInfo info = new DatabaseInfo()
                            {
                                Caption            = connection.DisplayName,
                                FromServerExplorer = true,
                                DatabaseType       = dbType,
                                ServerVersion      = SqliteEngineVersion,
                                ConnectionString   = sConnectionString
                            };
                            info.FileIsMissing = RepositoryHelper.IsMissing(info);
                            if (!databaseList.ContainsKey(sConnectionString))
                            {
                                databaseList.Add(sConnectionString, info);
                            }
                        }
                        if (includeServerConnections && objProviderGuid == new Guid(Resources.SqlServerDotNetProvider))
                        {
                            var sConnectionString = DataProtection.DecryptString(connection.EncryptedConnectionString);
                            var info = new DatabaseInfo()
                            {
                                Caption            = connection.DisplayName,
                                FromServerExplorer = true,
                                DatabaseType       = DatabaseType.SQLServer,
                                ServerVersion      = string.Empty,
                                ConnectionString   = sConnectionString
                            };
                            if (!databaseList.ContainsKey(sConnectionString))
                            {
                                databaseList.Add(sConnectionString, info);
                            }
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
            return(databaseList);
        }