internal static Dictionary <string, DatabaseInfo> GetOwnDataConnections()
        {
            var databaseList = new Dictionary <string, DatabaseInfo>();
            var dbType       = GetPreferredDatabaseType();
            var dbInfo       = new DatabaseInfo {
                ConnectionString = CreateStore(dbType), DatabaseType = dbType
            };

            using (var repository = Helpers.RepositoryHelper.CreateRepository(dbInfo))
            {
                var script  = "SELECT FileName, Source, CeVersion FROM Databases" + separator;
                var dataset = repository.ExecuteSql(script);
                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    var foundType = (DatabaseType)int.Parse(row[2].ToString());
                    if (!RepositoryHelper.IsV35Installed() && foundType == DatabaseType.SQLCE35)
                    {
                        continue;
                    }
                    if (!RepositoryHelper.IsV40Installed() && foundType == DatabaseType.SQLCE40)
                    {
                        continue;
                    }
                    var info = new DatabaseInfo();
                    try
                    {
                        info.Caption = GetFileName(row[1].ToString(), foundType);
                    }
                    catch
                    {
                        info.Caption = row[0].ToString();
                    }
                    var key = row[1].ToString();
                    info.DatabaseType       = foundType;
                    info.FromServerExplorer = false;
                    info.ConnectionString   = key;
                    info.ServerVersion      = "4.0.0.0";
                    if (foundType == DatabaseType.SQLCE35)
                    {
                        info.ServerVersion = "3.5.1.0";
                    }
                    if (foundType == DatabaseType.SQLite)
                    {
                        info.ServerVersion = RepositoryHelper.SqliteEngineVersion;
                    }
                    info.FileIsMissing = RepositoryHelper.IsMissing(info);
                    if (!databaseList.ContainsKey(key) && !info.FileIsMissing)
                    {
                        databaseList.Add(key, info);
                    }
                }
            }
            return(databaseList);
        }
        private static DatabaseType GetPreferredDatabaseType()
        {
            //Assume 3.5 is installed
            DatabaseType dbType = DatabaseType.SQLCE35;

            if (!RepositoryHelper.IsV35Installed()) //So 4.0 is installed
            {
                if (!RepositoryHelper.IsV40Installed())
                {
                    dbType = DatabaseType.SQLite;
                }
                else
                {
                    dbType = DatabaseType.SQLCE40;
                }
            }
            return(dbType);
        }
        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      = RepositoryHelper.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)
                    {
                    }
                    catch (ArgumentException)
                    {
                    }
                }
            }
#if SSMS
            try
            {
                if (package.TelemetryVersion().Major == 140 && Properties.Settings.Default.GetObjectExplorerDatabases)
                {
                    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 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;
                }
            }
        }
示例#5
0
        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);

            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;
                        }

                        if (info.DatabaseType != DatabaseType.SQLCE35 &&
                            !databaseList.ContainsKey(sConnectionString))
                        {
                            databaseList.Add(sConnectionString, info);
                        }
                    }
                    catch (KeyNotFoundException)
                    {
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
            return(databaseList);
        }
示例#6
0
        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);
        }