Esempio n. 1
0
        ///--------------------------------------------------------------------------------
        /// <summary>This loads information from a SqlServer database, using SMO.</summary>
        ///
        /// <param name="sqlDatabase">The input sql database</param>
        ///--------------------------------------------------------------------------------
        public void LoadSqlServerDatabase(Database sqlDatabase)
        {
            try
            {
                // load the basic database information
                SqlDatabaseName = sqlDatabase.Name;
                DbID            = sqlDatabase.ID;
                Owner           = sqlDatabase.Owner;
                try
                {
                    PrimaryFilePath        = sqlDatabase.PrimaryFilePath;
                    DefaultFileGroup       = sqlDatabase.DefaultFileGroup;
                    DefaultFullTextCatalog = sqlDatabase.DefaultFullTextCatalog;
                }
                catch
                {
                    // TODO: have specific Azure db load or identify Azure case
                }
                DefaultSchema = sqlDatabase.DefaultSchema;
                CreateDate    = sqlDatabase.CreateDate;
                Status        = sqlDatabase.Status.ToString();
                UserName      = sqlDatabase.UserName;
                State         = sqlDatabase.State.ToString();

                // load information for each property
                foreach (Microsoft.SqlServer.Management.Smo.Property loopProperty in sqlDatabase.Properties)
                {
                    if (DebugHelper.DebugAction == DebugAction.Stop)
                    {
                        return;
                    }
                    if (loopProperty.Expensive == false && loopProperty.IsNull == false && !String.IsNullOrEmpty(loopProperty.Value.ToString()))
                    {
                        SqlProperty property = new SqlProperty();
                        property.SqlPropertyID = Guid.NewGuid();
                        property.SqlDatabase   = this;
                        property.LoadProperty(loopProperty);
                        SqlPropertyList.Add(property);
                    }
                }

                try
                {
                    // load information for each extended property
                    foreach (ExtendedProperty loopProperty in sqlDatabase.ExtendedProperties)
                    {
                        if (DebugHelper.DebugAction == DebugAction.Stop)
                        {
                            return;
                        }
                        SqlExtendedProperty property = new SqlExtendedProperty();
                        property.SqlExtendedPropertyID = Guid.NewGuid();
                        property.SqlDatabase           = this;
                        property.LoadExtendedProperty(loopProperty);
                        SqlExtendedPropertyList.Add(property);
                    }
                }
                catch
                {
                    // TODO: have specific Azure db load or identify Azure case
                }

                // load information for each table
                foreach (Table loopTable in sqlDatabase.Tables)
                {
                    if (loopTable.IsSystemObject == true)
                    {
                        continue;
                    }
                    if (DebugHelper.DebugAction == DebugAction.Stop)
                    {
                        return;
                    }
                    SqlTable table = new SqlTable();
                    table.SqlTableID  = Guid.NewGuid();
                    table.SqlDatabase = this;
                    table.LoadTable(loopTable);
                    SqlTableList.Add(table);
                }

                // load information for each view
                foreach (Microsoft.SqlServer.Management.Smo.View loopView in sqlDatabase.Views)
                {
                    if (loopView.IsSystemObject == true)
                    {
                        continue;
                    }
                    SqlView view = new SqlView();
                    view.SqlViewID   = Guid.NewGuid();
                    view.SqlDatabase = this;
                    view.LoadView(loopView);
                    SqlViewList.Add(view);
                }
            }
            catch (ApplicationAbortException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        ///--------------------------------------------------------------------------------
        /// <summary>This loads information from a MySQL database.</summary>
        ///
        /// <param name="sqlConnection">The input sql connection</param>
        ///--------------------------------------------------------------------------------
        public void LoadMySQLDatabase(MySqlConnection sqlConnection)
        {
            try
            {
                // load the basic database information
                SqlDatabaseName = sqlConnection.Database;
                //Owner = sqlDatabase.Owner;
                //PrimaryFilePath = sqlDatabase.PrimaryFilePath;
                //DefaultSchema = sqlDatabase.DefaultSchema;
                //DefaultFileGroup = sqlDatabase.DefaultFileGroup;
                //CreateDate = sqlDatabase.CreateDate;

                // load variables
                NameObjectCollection variables = new NameObjectCollection();
                MySqlCommand         command   = sqlConnection.CreateCommand();
                command.CommandText = "SHOW VARIABLES;";
                MySqlDataReader Reader;
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    variables[Reader.GetValue(0).ToString()] = Reader.GetValue(1).ToString();
                }
                Reader.Close();

                // add variables to database properties
                foreach (string variable in variables.AllKeys)
                {
                    if (DebugHelper.DebugAction == DebugAction.Stop)
                    {
                        return;
                    }
                    SqlProperty property = new SqlProperty();
                    property.SqlPropertyID = Guid.NewGuid();
                    property.SqlDatabase   = this;
                    property.LoadMySQLProperty(variable, null, variables[variable].ToString());
                    SqlPropertyList.Add(property);
                }

                // load tables of schema info
                DataTable tables            = null;
                DataTable columns           = null;
                DataTable indexes           = null;
                DataTable indexColumns      = null;
                DataTable foreignKeys       = null;
                DataTable foreignKeyColumns = null;
                try
                {
                    tables = sqlConnection.GetSchema("Tables");
                }
                catch
                {
                    Solution.ShowIssue(String.Format(DisplayValues.Exception_MySQLSchemaLoad, "Tables", SqlDatabaseName), null, Solution.IsSampleMode);
                }
                try
                {
                    columns = sqlConnection.GetSchema("Columns");
                }
                catch
                {
                    Solution.ShowIssue(String.Format(DisplayValues.Exception_MySQLSchemaLoad, "Columns", SqlDatabaseName), null, Solution.IsSampleMode);
                }
                try
                {
                    indexes = sqlConnection.GetSchema("Indexes");
                }
                catch
                {
                    Solution.ShowIssue(String.Format(DisplayValues.Exception_MySQLSchemaLoad, "Indexes", SqlDatabaseName), null, Solution.IsSampleMode);
                }
                try
                {
                    indexColumns = sqlConnection.GetSchema("IndexColumns");
                }
                catch
                {
                    Solution.ShowIssue(String.Format(DisplayValues.Exception_MySQLSchemaLoad, "IndexColumns", SqlDatabaseName), null, Solution.IsSampleMode);
                }
                try
                {
                    foreignKeys = sqlConnection.GetSchema("Foreign Keys");
                }
                catch
                {
                    Solution.ShowIssue(String.Format(DisplayValues.Exception_MySQLSchemaLoad, "Foreign Keys", SqlDatabaseName), null, Solution.IsSampleMode);
                }
                try
                {
                    foreignKeyColumns = sqlConnection.GetSchema("Foreign Key Columns");
                }
                catch
                {
                    Solution.ShowIssue(String.Format(DisplayValues.Exception_MySQLSchemaLoad, "Foreign Key Columns", SqlDatabaseName), null, Solution.IsSampleMode);
                }

                // load information for each table
                if (tables != null)
                {
                    foreach (DataRow row in tables.Rows)
                    {
                        if (DebugHelper.DebugAction == DebugAction.Stop)
                        {
                            return;
                        }
                        SqlTable table = new SqlTable();
                        table.SqlTableID  = Guid.NewGuid();
                        table.SqlDatabase = this;
                        table.LoadMySQLTable(sqlConnection, variables, row, columns, indexes, indexColumns, foreignKeys, foreignKeyColumns);
                        SqlTableList.Add(table);
                    }
                }
            }
            catch (ApplicationAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                bool reThrow = BusinessConfiguration.HandleException(ex);
                if (reThrow)
                {
                    throw;
                }
            }
        }