public static void DeleteDbIfExists(SqlConnectionStringBuilder csB)
        {
            if (SqlUtilities.DatabaseExist(csB))
            {
                string dbName;

                dbName = csB.InitialCatalog;


                try
                {
                    csB.InitialCatalog = "";

                    if (!SqlUtilities.IsDatabaseConnected(csB.ConnectionString, dbName))
                    {
                        csB.InitialCatalog = dbName;

                        SqlUtilities.DropDatabase(csB);
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format("Drop database failed. Users are connected to database {0}", dbName));
                    }
                }
                finally
                {
                    csB.InitialCatalog = dbName;
                }
            }
        }
Beispiel #2
0
        public static void DeleteDbIfExists(SqlConnectionStringBuilder csB, int callbackFrequencyInMilliseconds, DatabaseCallback cb)
        {
            if (SqlUtilities.DatabaseExist(csB))
            {
                bool   databaseDropped = false;
                string dbName;
                string fop;

                dbName = csB.InitialCatalog;
                fop    = csB.FailoverPartner;

                try
                {
                    csB.InitialCatalog  = string.Empty;
                    csB.FailoverPartner = string.Empty;

                    SqlUtilities.CloseDatabaseConnections(csB.ConnectionString, dbName);

                    if (!SqlUtilities.IsDatabaseConnected(csB.ConnectionString, dbName))
                    {
                        csB.InitialCatalog  = dbName;
                        csB.FailoverPartner = fop;

                        SqlUtilities.DropDatabase(csB, callbackFrequencyInMilliseconds, cb);
                        databaseDropped = true;
                    }
                }
                finally
                {
                    csB.InitialCatalog  = dbName;
                    csB.FailoverPartner = fop;
                }

                if (databaseDropped == false)
                {
                    try
                    {
                        csB.InitialCatalog  = dbName;
                        csB.FailoverPartner = fop;

                        SqlUtilities.DropDatabase(csB, callbackFrequencyInMilliseconds, cb);
                        databaseDropped = true;
                    }
                    catch (Exception)
                    {
                        throw new InvalidOperationException(string.Format("Drop database failed. Users are connected to database {0}", dbName));
                    }
                }
            }
        }