private void CleaningUpPreviousDb()
 {
     Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(connectionStringBuilder.DataSource);
     if (server.Databases.Contains(connectionStringBuilder.InitialCatalog))
     {
         server.KillAllProcesses(connectionStringBuilder.InitialCatalog);
         server.DetachDatabase(connectionStringBuilder.InitialCatalog, true);
     }
 }
Пример #2
0
        public bool InitialDBInstance(String dbInstanceName, String dbName, Int32 interval, Int32 timeout, String uid, String dbFilePath)
        {
            bool sqlServerAlive = false;

            SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder();

            sqlConnectionStringBuilder.DataSource         = dbInstanceName;
            sqlConnectionStringBuilder.InitialCatalog     = dbName;
            sqlConnectionStringBuilder.ConnectTimeout     = interval;
            sqlConnectionStringBuilder.IntegratedSecurity = true;   //Windows认证

            if (File.Exists(dbFilePath))
            {
                sqlConnectionStringBuilder.AttachDBFilename = dbFilePath;
                Microsoft.SqlServer.Management.Smo.Server server = null;
                bool  retry = true;
                Int32 SQLSERVER_CONNECT_RETRY_COUNT    = 10;
                Int32 SQLSERVER_CONNECT_RETRY_INTERVAL = 1000;
                Int32 connectTryCount = 0;
                while (retry)
                {
                    connectTryCount++;
                    try
                    {
                        server         = new Microsoft.SqlServer.Management.Smo.Server(dbInstanceName);
                        retry          = false;
                        sqlServerAlive = server.Databases.Contains(dbName);
                    }
                    catch (Exception ex)
                    {
                        retry = connectTryCount <= SQLSERVER_CONNECT_RETRY_COUNT;
                        Thread.Sleep(SQLSERVER_CONNECT_RETRY_INTERVAL);
                        //日志
                    }
                }

                if (sqlServerAlive)
                {
                    try
                    {
                        server.DetachDatabase(dbName, false);
                    }
                    catch (Exception ex)
                    {
                        //日志
                    }
                }
            }
            else
            {
                //
            }

            this.m_connectionString = sqlConnectionStringBuilder.ConnectionString;
            return(sqlServerAlive);
        }