C# 如何以编程方式获取 SQL Server 安装路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1165576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:47:09  来源:igfitidea点击:

C# How to get SQL Server installation path programatically?

c#sql-server

提问by Julius A

How do I get the installation path for a given instance of SQL Server (default and name instances)

如何获取给定 SQL Server 实例的安装路径(默认和名称实例)

采纳答案by Julius A

using(RegistryKey sqlServerKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"))
{
    foreach (string subKeyName in sqlServerKey.GetSubKeyNames())
    {
        if(subKeyName.StartsWith("MSSQL."))
        {
            using(RegistryKey instanceKey = sqlServerKey.OpenSubKey(subKeyName))
            {
                string instanceName = instanceKey.GetValue("").ToString();

                if (instanceName == "MSSQLSERVER")//say
                {
                    string path = instanceKey.OpenSubKey(@"Setup").GetValue("SQLBinRoot").ToString();
                    path = Path.Combine(path, "sqlserver.exe");
                    return path;
                }
            }
        }
    }
}

回答by George Stavrou

If you have the connection string, you may select the Directory with SQL

如果您有连接字符串,您可以选择带有 SQL 的目录

private string ServerRootDirectory(string connString)
    {
        string path = string.Empty;
        using (SqlConnection con = new SqlConnection(connString))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = string.Format(@"DECLARE @InstanceName varchar(100), 
                                                        @InstanceLocation varchar(100),
                                                        @InstancePath varchar(100)

                                                SELECT @InstanceName = convert(varchar, ServerProperty('InstanceName'))
                                                EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
                                                    @key='Software\Microsoft\Microsoft SQL Server\Instance Names\SQL',
                                                    @value_name=@InstanceName,
                                                    @value=@InstanceLocation OUTPUT
                                                SELECT @InstanceLocation = 'Software\Microsoft\Microsoft SQL Server\'+@InstanceLocation+'\Setup'

                                                EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
                                                    @key=@InstanceLocation,
                                                    @value_name='SQLPath',
                                                    @value=@InstancePath OUTPUT
                                                SELECT @InstancePath as RootDirectoryPath");
            path = (string)cmd.ExecuteScalar();
            con.Close();
        }
        return path;
    }

Output of the above code:

上面代码的输出:

c:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL

c:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL