c++ - 如何知道Windows XP 或7 中是否安装了MSSql Server?

标签 c++ sql-server windows mfc

在安装MSSQLSERVER之前,我想知道系统中是否已经安装了MSSQLSERVER。这应该在 C++ 或 mfc 中以编程方式完成。

如果 Windows 中已经安装了 MSSQLServer,那么是否有任何可能的方法来获取该服务器的 MSSql 凭据?

如果是,请解释实现这一点的方法?

更新

我尝试在我的系统中安装 sqlserver 2005 和 sql server 2008,但两个服务器都已安装。我检查了注册表路径,但它包含 SOFTWARE\Microsoft\Microsoft SQL Server\90 for MSSQLServer 2005SOFTWARE\Microsoft\Microsoft SQL Server\100 对于 MSSqlServer 2008。在安装我的 MSSQLServer 之前 我如何检查是否安装了任何 MSSql Server 版本?

编辑

直到现在,我都在 C++ 中以编程方式静默安装了 SQLSERVER。

我发布了下面的代码:

SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = L"D:\\Softies\\SQLEXPR.EXE";
ShExecInfo.lpParameters = L"/qn addlocal=all InstanceName=SQLEXPRESS DisableNetworkProtocols=0 SECURITYMODE=SQL SAPWD=root SQLAUTOSTART=1 SQLBROWSERAUTOSTART=1 ENABLERANU=0";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_MAXIMIZE;
ShExecInfo.hInstApp = NULL;

ShellExecuteEx(&ShExecInfo);

int nResult=0;
nResult = (int )ShExecInfo.hInstApp;
if( nResult >32) 
cout<<"EXE executed successfully"<<endl;
else
cout<<"Reason for failure is" <<nResult<<endl;
return 0;

最佳答案

  • 您无法获取服务器的凭据,暴力破解除外

但是

如果您是本地管理员 - 您可以使用启动键 -f 从命令行启动 sql server 并尝试使用它 - 不能保证,但您可以尝试。

  • 我有一个代码可以检查 sql server 实例的一些默认值,因此它可以很容易地适应您的需要和 c++/

    private void GetSqlDefaultInfo(string ServerName, string InstanceName)
    {
    
        InstanceName = string.IsNullOrEmpty(InstanceName) ? "MSSQLSERVER" : InstanceName;
    
        if (string.IsNullOrEmpty(ServerName))
            ServerName = Environment.MachineName;
        using (var registryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName))
        {
            object sqlInstance;
            using (var subKey = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"))
                sqlInstance = subKey.GetValue(InstanceName);
            if (sqlInstance != null && !string.IsNullOrEmpty(sqlInstance.ToString()))
            {
                var sqlPathKey = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\MSSQLServer",
                                               sqlInstance);
                object defaultData, defaultLog, backupDirectory, sqlPath;
                using (var subKey = registryKey.OpenSubKey(sqlPathKey))
                {
                    defaultData = subKey.GetValue("DefaultData");
                    defaultLog = subKey.GetValue("DefaultLog");
                    backupDirectory = subKey.GetValue("BackupDirectory");
                }
                sqlPathKey = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", sqlInstance);
    
                using (var subKey = registryKey.OpenSubKey(sqlPathKey))
                    sqlPath = subKey.GetValue("SQLDataRoot");
                DataFilePath = defaultData != null
                                   ? defaultData.ToString()
                                   : Path.Combine(sqlPath.ToString(), "Data").TrimEnd('\\');
    
                LogFilePath = defaultLog != null
                                  ? defaultLog.ToString()
                                  : Path.Combine(sqlPath.ToString(), "Data").TrimEnd('\\');
                FTSIndexFilePath = DataFilePath;
                ContentFilePath = DataFilePath;
                BackupFilePath = backupDirectory != null
                                     ? backupDirectory.ToString()
                                     : Path.Combine(sqlPath.ToString(), "Backup").TrimEnd('\\');
            }
        }
    }
    

关于c++ - 如何知道Windows XP 或7 中是否安装了MSSql Server?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8697261/

相关文章:

sql-server - SQL Server Reporting Services 中的 Oracle 日期格式异常

mysql - sql查询提取唯一记录

windows - WiX Bootstrapper 在退出时运行可执行文件

c++ - 使用 DFT 进行卷积

c++ - Omnet Tkenv 为多个参数运行配置 : executing only the first value of parameter

mysql - 从 MySQL 迁移到 SQL Server 时出现复杂查询的意外结果?

python - 使用 Python 3.8 的 Jupyter Notebook - NotImplementedError

c++ - 是否可以在 Cocos2d-x 中更改标签字体大小?

c++ - 链接 : fatal error LNK1104: cannot open file '..\json_spirit\Debug\json_spirit_lib.lib'

c++ - 在 Windows 中的不同位置初始化变量会出错