Skip to content Skip to sidebar Skip to footer

How Do I Get Sql Server Instance Path Using Vb.net Code

I need to get the folder with full path of my SQL Server instance installed on my machine, using vb code. For example, I have installed instance MyComputer\MyInstanceName. I know i

Solution 1:

It's not all that obvious, but you can find everything you're looking for in the registry.

Basically:

  • you need to inspect the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL subkey to find the installed instances and their "internal" names

  • with that internal name, you can inspect the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\(internal name)\Setup node to find everything you might want to know about that instance

In C# code (and for a 64-bit OS, with 64-bit versions of SQL Server), this would be:

// open the 64-bit view of the registry, if you're using a 64-bit OS
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

// find the installed SQL Server instance names
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");

// loop over those instancesforeach (string sqlInstance in key.GetValueNames())
{
    Console.WriteLine("SQL Server instance: {0}", sqlInstance);

    // find the SQL Server internal name for the instancestring internalName = key.GetValue(sqlInstance).ToString();
    Console.WriteLine("\tInternal instance name: {0}", internalName);

    // using that internal name - find the "Setup" node in the registrystring instanceSetupNode = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", internalName);

    RegistryKey setupKey = baseKey.OpenSubKey(instanceSetupNode, false);

    if (setupKey != null)
    {
        // in the "Setup" node, you have several interesting items, like// * edition and version of that instance// * base path for the instance itself, and for the data for that instancestring edition = setupKey.GetValue("Edition").ToString();
        string pathToInstance = setupKey.GetValue("SQLBinRoot").ToString();
        string version = setupKey.GetValue("Version").ToString();

        Console.WriteLine("\tEdition         : {0}", edition);
        Console.WriteLine("\tVersion         : {0}", version);
        Console.WriteLine("\tPath to instance: {0}", pathToInstance);
    }
}

Post a Comment for "How Do I Get Sql Server Instance Path Using Vb.net Code"