gpt4 book ai didi

c# - 在 sql server 2005、2008、2012 上使用 smo 进行数据库收集

转载 作者:行者123 更新时间:2023-11-30 22:21:38 26 4
gpt4 key购买 nike

我正在尝试使用 smo 获取数据库集合,如下所示:

var server = GetDatabaseServer();

DatabaseCollection databaseCollection = server.Databases;

foreach (Database database in databaseCollection)
{
if (String.Equals(database.Name, databseName,
StringComparison.OrdinalIgnoreCase))
{
if (database.IsSystemObject == false &&
database.IsMirroringEnabled == false)
{
if (IsTimeForBackup(database.LastBackupDate))
Backup(database, server, backupDirectory);
break;
}
}
}

如果 SqlServer 是 2005 或 2008,这将起作用。如果我尝试连接到 2012 的实例,我在检查数据库时遇到异常。IsMirroringEnabled:

索引超出了数组的范围。

The stack trace is : at Microsoft.SqlServer.Management.Smo.SqlPropertyMetadataProvider.PropertyNameToIDLookupWithException(String propertyName, PropertyAccessPurpose pap) at Microsoft.SqlServer.Management.Smo.PropertyCollection.LookupID(String propertyName, PropertyAccessPurpose pap) at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName, Boolean throwOnNullValue) at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName) at Microsoft.SqlServer.Management.Smo.Database.get_IsSystemObject() at RBC.StockManagement.CommunicationService.DatabaseBackup.BackupDatabase(String databseName, String backupDirectory) in D:\Work\Trunk\RBC.StockManagement\RBC.StockManagement.CommunicationService\DatabaseBackup.cs:line 85

我要备份的数据库是从 SqlServer 2005 创建的,我的操作系统在 x64 架构上运行。 2005(developer edition)、2008(express edition)和2012(express edition)这三个实例都在同一台计算机上。

最佳答案

this MSDN article看SQL Server 2012 附带了新的 SMO DLL,如果您尝试使用旧实例连接到 2012 实例,它将无法正常工作。

References to SMO dlls in older versions of SQL Server must be removed, and references to the new SMO dlls that are provided with SQL Server 2012 must be included.

Minimally, you would reference the following:

  • Microsoft.SqlServer.ConnectionInfo

  • Microsoft.SqlServer.Smo

  • Microsoft.SqlServer.Management.Sdk.Sfc

This related Connect article特别提到了您尝试连接时遇到的错误 - 所以我猜这是同一个问题。 MS 在那里说他们不会更新旧的以允许他们连接到 2012,所以我想更新到新的 SMO DLL 是你最好的选择。

来自常规 SO 贡献者 Aaron bertrand:

See this item for Microsoft's official stance about forward compatibility (not going to fix) and backward compatibility (Denali SSMS will be able to manage 2000, 2005, 2008, 2008 R2): http://connect.microsoft.com/SQLServer/feedback/details/622441

(Denali 是 2012 年发布前的代号,以防您不知道)。

关于c# - 在 sql server 2005、2008、2012 上使用 smo 进行数据库收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14232305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com