SMO相当于SET HADR OFF

时间:2015-01-14 18:38:22

标签: c# sql-server-2012 smo alwayson

在SMO(C#)中,等同于......

ALTER DATABASE db SET HADR OFF;

我已经查看了Database和AvailabilityDatabase类的文档,但没有任何内容真正跳出来。

此处的最终目标是删除作为可用性组成员的数据库。目前,我们首先关闭HADR,然后在所有辅助服务器上删除数据库,然后在主服务器上从可用性组中删除数据库并将数据库放在那里。

1 个答案:

答案 0 :(得分:1)

以下是我使用SMO删除加入AlwaysOn可用性组的数据库的过程......

Server primaryServer = new Server();
AvailabilityDatabase pDb = primaryServer.AvailabilityGroups[agName].AvailabilityDatabases[dbName];
pDb.SuspendDataMovement();
while (!pDb.IsSuspended)
{
  Thread.Sleep(1000);
  pDb.Refresh();
}

foreach (var secondary in secondaryServers)
{
  AvailabilityDatabase sDb = secondary.AvailabilityGroups[agName].AvailabilityDatabases[dbName];
  sDb.SuspendDataMovement();
  while (!sDb.IsSuspended)
  {
    Thread.Sleep(1000);
    sDb.Refresh();
  }

  sDb.LeaveAvailabilityGroup(); // this appears to be the equivalent of SET HADR OFF

  Database db = secondary.Databases[dbName];
  db.UserAccess = DatabaseUserAccess.Single;
  secondary.KillAllProcesses(dbName);
  db.Drop();
}

pDb.Drop();

Database db = primaryServer.Databases[dbName];
db.UserAccess = DatabaseUserAccess.Single;
primaryServer.KillAllProcesses(dbName);
db.Drop();