Пример #1
0
        public void TestSetup()
        {
            //start with default options
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            string[] options = { connectionString };
            RunOptions.Current.Logger = new TestLogger();
            RunOptions.Current.Init(options);

            SqlConnection    connection       = new SqlConnection(connectionString);
            ServerConnection serverConnection = new ServerConnection(connection);
            Server           server           = new Server(serverConnection);

            Microsoft.SqlServer.Management.Smo.Database database = server.Databases[connection.Database];
            if (database == null)
            {
                throw new ApplicationException("Database " + connection.Database + " not found.");
            }

            server.SetDefaultInitFields(typeof(View), true);
            database.PrefetchObjects(typeof(View));
            for (int i = database.Views.Count - 1; i >= 0; i--)
            {
                View view = database.Views[i];

                if (view.IsSystemObject)
                {
                    continue;
                }

                view.Drop();
            }

            server.SetDefaultInitFields(typeof(Table), true);
            database.PrefetchObjects(typeof(Table));
            foreach (Table table in database.Tables)
            {
                if (table.IsSystemObject)
                {
                    continue;
                }

                for (int i = table.ForeignKeys.Count - 1; i >= 0; i--)
                {
                    table.ForeignKeys[i].Drop();
                }
            }

            for (int i = database.Tables.Count - 1; i >= 0; i--)
            {
                Table table = database.Tables[i];

                if (table.IsSystemObject)
                {
                    continue;
                }

                table.Drop();
            }

            server.SetDefaultInitFields(typeof(StoredProcedure), true);
            database.PrefetchObjects(typeof(StoredProcedure));
            for (int i = database.StoredProcedures.Count - 1; i >= 0; i--)
            {
                StoredProcedure procedure = database.StoredProcedures[i];

                if (procedure.IsSystemObject)
                {
                    continue;
                }

                procedure.Drop();
            }

            server.SetDefaultInitFields(typeof(UserDefinedFunction), true);
            database.PrefetchObjects(typeof(UserDefinedFunction));
            for (int i = database.UserDefinedFunctions.Count - 1; i >= 0; i--)
            {
                UserDefinedFunction function = database.UserDefinedFunctions[i];

                if (function.IsSystemObject)
                {
                    continue;
                }

                function.Drop();
            }

            server.SetDefaultInitFields(typeof(UserDefinedTableType), true);
            database.PrefetchObjects(typeof(UserDefinedTableType));
            for (int i = database.UserDefinedTableTypes.Count - 1; i >= 0; i--)
            {
                UserDefinedTableType tableType = database.UserDefinedTableTypes[i];

                if (!tableType.IsUserDefined)
                {
                    continue;
                }

                tableType.Drop();
            }

            server.SetDefaultInitFields(typeof(FullTextCatalog), true);
            for (int i = database.FullTextCatalogs.Count - 1; i >= 0; i--)
            {
                FullTextCatalog catalog = database.FullTextCatalogs[i];

                catalog.Drop();
            }
        }