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);
        }
        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);
        }
Ejemplo n.º 3
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);
        }