如何使用C ++获取SQLSERVER安装路径? [英] How to get the SQLSERVER installationPath using C++?

查看:257
本文介绍了如何使用C ++获取SQLSERVER安装路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式获取Sqlserver安装路径。

How to get the Sqlserver Installation Path Programatically.

如果我们去注册表然后注册表配置可能会不同于一个操作系统到其他操作系统

If we go for the Registry Then Registry Hives May be Different from one OS to other OS

像w2k3到w3k8和VISTA和W2k8 R2喜欢这样..

Like w2k3 to w3k8 and VISTA and W2k8 R2 like that..

但我使用注册表如下,但我给了一些硬代码密钥vAlues如果Hives是相同的,没有问题,如果不同

But i did Using Registry as follows But I gave Some Hard Code Key vAlues If Hives are same then no problem if Different The Problem rises....

    CRegKey cregkey;
DWORD result;
const char subkey[]="SOFTWARE\\Microsoft\\Microsoft SQL Server\\100";
const char keyval[] = "VerSpecificRootDir";
char path[ 2048 ];
result = cregkey.Open(HKEY_LOCAL_MACHINE,subkey);
    if (ERROR_SUCCESS == result)
    {
    	DWORD dwCount = sizeof( path );
	    result = cregkey.QueryStringValue(keyval, path, &dwCount);
          if (ERROR_SUCCESS == result) 
           {			  
                cout<<" The SQLSERVER Pathis "<<path<<endl;

           }
   }


推荐答案

我假设实例独立设置(即不依赖于运行的SQL Server实例的共享工具)的hive应该是HKEY_LOCAL_MACHINE\SOFTWARE\Microsft\ Microsoft SQL Server (版本号)。如果你找不到工具HKEY_LOCAL_MACHINE \SOFTWARE \Microsoft \Microsoft SQL Server \100版本(它匹配SQL Server 2008),也许你可以检查90版本(SQL 2005)HKEY_LOCAL_MACHINE \SOFTWARE \Microsoft \Microsoft SQL Server \ 90?

I suppose that hive for Instance independent settings (i.e. shared tools that do not depend on how many instances of SQL Server you run) should be HKEY_LOCAL_MACHINE\SOFTWARE\Microsft\Microsoft SQL Server(version number). If you cannot find tools for HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100 version (which match SQL Server 2008), perhaps you can check 90 version (SQL 2005) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\90 ?

在某些版本的Windows上,你知道这个蜂巢是不同的吗?

Have you ever seen that on some version of Windows this hive is different?


这里也是一篇文章,解释如何获取Instance感知的注册表项
MSDN:SQL Server的默认和命名实例的文件位置
在我们的安装脚本中,我们使用HKEY_LOCAL_MACHINE \\ \\ SOFTWARE \Microsoft \Microsoft SQL Server \MSSQL.1 \Setup,例如。其中MSSQL.1是一个实例名称,实例名称可以在HKEY_LOCAL_MACHINE \SOFTWARE\Microsoft\ Microsoft SQL Server \实例名称

Here is also an article that explain how to get Instance-aware registry keys MSDN: File Locations for Default and Named Instances of SQL Server In our installation script we use values from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup, for example. where MSSQL.1 is an instance name, and instance names can be found in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names

几个文章,建议从HKEY_LOCAL_MACHINE \Software \Microsoft\MSSQLServer\Setup
我不能在Windows XP上工作,但它适用于Win 2003 Srv
请参阅sql-server-how-to-select-the-installation-path

Also I saw several articles that recommend reading from HKEY_LOCAL_MACHINE\Software\Microsoft\MSSQLServer\Setup I will not work on Windows XP, but it works on Win 2003 Srv See sql-server-how-to-select-the-installation-path

所以你没有办法,但是实现更复杂的逻辑 - 首先检查HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup如果不存在,检查HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft \ Microsoft SQL Server \MSSQL.1 \Setup ...但这是你需要的实例感知文件夹,如Binn,数据,备份...

So you have no way probably but to implement more complex logic - first check HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup if not exists, check HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup... But this is what you will need for instance-aware folders, like Binn, Data, Backup...

这篇关于如何使用C ++获取SQLSERVER安装路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆