检测 SMO 存在的最佳方法是什么?

发布于 2024-07-04 21:33:09 字数 380 浏览 5 评论 0原文

我有一些代码使用 SMO 来填充可用 SQL Server 和数据库的列表。 虽然我们不再支持 SQL Server 2000,但代码有可能在 SQL Server 2000 且未安装 SMO 库的计算机上运行。 我宁愿先检查 SMO 并优雅地降低功能,而不是在用户面前爆炸。 检测机器上是否可用 SMO 的最佳方法是什么?

我通过 Google 快速扫描看到的每个示例都是“查找 C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll”的变体。 该方法的问题在于它仅适用于 SQL Server 2005。如果 SQL Server 2008 是唯一安装的 SQL Server,则路径将会不同。

I have some code that uses SMO to populate a list of available SQL Servers and databases. While we no longer support SQL Server 2000, it's possible that the code could get run on a machine that SQL Server 2000 and not have the SMO library installed. I would perfer to check for SMO first and degrade the functionality gracefully instead of blowing up in the user's face. What is best way to detect whether or not SMO is available on a machine?

Every example that I have seen through a quick Google scan was a variation of "look for C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll". The problem with that approach is that it only works with SQL Server 2005. If SQL Server 2008 is the only SQL Server installed then the path will be different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡紫姑娘! 2024-07-11 21:33:09

简单说明一下:
HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\SharedManagementObjects\CurrentVersion\Version 并不代表当前安装的版本,因为可能安装了多个版本。

上面的注册表项会在您安装版本时更新,因此如果您安装了 SMO 2014,那么您应该看到 12.x,但如果之后您安装 SMO 2012,那么该版本将更改为 11.x
如果您随后决定修复 2014 年安装,那么版本将再次为 12.x

您应该更好地查看:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server 2012 Redist\SharedManagementObjects\1033\CurrentVersion

或 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server 2014 Redist\SharedManagementObjects\1033\CurrentVersion

有人知道 1033 是否得到保证吗?
(仅指英文版)

Just a quick note:
HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\SharedManagementObjects\CurrentVersion\Version doesn't represent the current version that is installed, because there could be several versions installed.

The registry key above is being updated when you install a version, so if you've installed SMO 2014 then you should see 12.x, but if afterwards you install SMO 2012, then this version would change to 11.x
If you then decides to repair the 2014 installtion, then the version would be again 12.x

You should better look at:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server 2012 Redist\SharedManagementObjects\1033\CurrentVersion

or HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server 2014 Redist\SharedManagementObjects\1033\CurrentVersion

Does someone knows if the 1033 is guaranteed?
(Meaning only english version)

鹿童谣 2024-07-11 21:33:09

SQL Server 2012的解决方案:

HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\SharedManagementObjects\CurrentVersion\Version

您可以检查该键是否存在(并检查该值是否大于11)。

Solution for SQL Server 2012:

HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\SharedManagementObjects\CurrentVersion\Version

You can check if this key exists (and check if the value is greater than 11).

甜柠檬 2024-07-11 21:33:09

我所做的只是尝试创建某个 SMO 对象的实例。 如果失败了,它就不存在了。

What I do is just try to create an instance of some SMO object. If it fails, its not there.

九厘米的零° 2024-07-11 21:33:09

这有点笨拙,但快速检查注册表似乎可行。 在 HKEY_CLASSES_ROOT 下,将注册来自 SMO 程序集的大量类。 我所需要做的就是选择一个 SMO 类并检查是否存在同名的密钥。 如果已安装 SMO,则以下函数将返回 true,否则返回 false。

private bool CheckForSmo()
{
    string RegKeyName = @"Microsoft.SqlServer.Management.Smo.Database";
    bool result = false;
    Microsoft.Win32.RegistryKey hkcr = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(RegKeyName);
    result = hkcr != null;

    if (hkcr != null)
    {
        hkcr.Close();
    }

    return result;
}

This is kind of clunky, but a quick check of the registry seems to work. Under HKEY_CLASSES_ROOT, a large number of classes from the SMO assemblies will be registered. All I needed to do was to pick one of the SMO classes and check for the existence of the key with the same name. The following function will return true if SMO has been installed, false if otherwise.

private bool CheckForSmo()
{
    string RegKeyName = @"Microsoft.SqlServer.Management.Smo.Database";
    bool result = false;
    Microsoft.Win32.RegistryKey hkcr = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(RegKeyName);
    result = hkcr != null;

    if (hkcr != null)
    {
        hkcr.Close();
    }

    return result;
}
半﹌身腐败 2024-07-11 21:33:09

我查看了 SQL2008 R2 功能包中的 SharedManagementObjects.msi 和我的 Windows 注册表(SQL2008 R2 Dev 安装在这台计算机上),我相信这些是应该用来检测 SMO 的注册表项(全部位于 HKLM 下)

: \Microsoft\Microsoft SQL Server\SharedManagementObjects\CurrentVersion - 这显然是主键,表明安装了某些版本的 SMO。

SOFTWARE\Microsoft\Microsoft SQL Server 2008 Redist\SharedManagementObjects\1033\CurrentVersion - 这可能意味着安装了 2008 英文版本。 可能只需检查 SOFTWARE\Microsoft\Microsoft SQL Server 2008 Redist\SharedManagementObjects 是否存在就足够了。

同样适用于 SQL2012:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server 2012 Redist\SharedManagementObjects\1033\CurrentVersion

但不是 SQL2005! 即使我在这台机器上也安装了 2005。

另一件事是,您通常还需要 Microsoft SQL Server 系统 CLR 类型,因为 SMO 依赖于它们。
SQLSysClrTypes.msi 只有一个注册表项:
软件\Microsoft\Microsoft SQL Server\RefCount\SQLSysClrTypes

I had a look at the SharedManagementObjects.msi from the SQL2008 R2 feature pack and my Windows Registry (SQL2008 R2 Dev is installed on this machine) and I believe these are the reg keys one should use to detect SMO (All under HKLM):

SOFTWARE\Microsoft\Microsoft SQL Server\SharedManagementObjects\CurrentVersion - this is apparently the main key, indicating that some version of SMO is installed.

SOFTWARE\Microsoft\Microsoft SQL Server 2008 Redist\SharedManagementObjects\1033\CurrentVersion - this one probably means 2008 English is installed. Probably just checking for the presence of SOFTWARE\Microsoft\Microsoft SQL Server 2008 Redist\SharedManagementObjects would suffice.

Same applies to SQL2012:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server 2012 Redist\SharedManagementObjects\1033\CurrentVersion

But NOT SQL2005! even though I do have 2005 installed on this machine as well.

One more thing, You'd normally want Microsoft SQL Server System CLR Types as well, since SMO depends on them.
The SQLSysClrTypes.msi has only one registry key:
SOFTWARE\Microsoft\Microsoft SQL Server\RefCount\SQLSysClrTypes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文