Exemplo n.º 1
0
        private static void UpdateCreateDatabaseCallback(DatabaseCallback cb, DatabaseArgs args, DatabaseState state)
        {
            args.state = state;
            if (args.state == DatabaseState.Starting)
            {
                args.elapsedMilliseconds = 0;
            }

            if (cb != null)
            {
                cb(args);
            }
        }
Exemplo n.º 2
0
        public static void DropDatabase(SqlConnectionStringBuilder connectionBuilder, int callbackFrequencyInMilliseconds, DatabaseCallback cb)
        {
            try
            {
                string databaseName = connectionBuilder.InitialCatalog;
                bool   pooling      = connectionBuilder.Pooling;
                string fop          = connectionBuilder.FailoverPartner;

                connectionBuilder.InitialCatalog  = string.Empty;
                connectionBuilder.Pooling         = false;
                connectionBuilder.FailoverPartner = string.Empty;

                using (SqlConnection connection = new SqlConnection(connectionBuilder.ConnectionString))
                {
                    string     createDatabaseString = string.Format(Constants.SqlScript.DropDatabase, databaseName);
                    SqlCommand command = new SqlCommand(createDatabaseString, connection);

                    connectionBuilder.InitialCatalog  = databaseName;
                    connectionBuilder.Pooling         = pooling;
                    connectionBuilder.FailoverPartner = fop;

                    connection.Open();

                    DatabaseArgs args = new DatabaseArgs();
                    UpdateCreateDatabaseCallback(cb, args, DatabaseState.Starting);

                    IAsyncResult result = command.BeginExecuteNonQuery();
                    while (!result.IsCompleted)
                    {
                        System.Threading.Thread.Sleep(callbackFrequencyInMilliseconds);
                        args.elapsedMilliseconds = args.elapsedMilliseconds + callbackFrequencyInMilliseconds;
                        UpdateCreateDatabaseCallback(cb, args, DatabaseState.Working);
                    }

                    command.EndExecuteNonQuery(result);
                    UpdateCreateDatabaseCallback(cb, args, DatabaseState.Finished);
                }
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.Assert(false);

                throw exception;
            }
        }