web-dev-qa-db-fra.com

SQL Server: comment trouver tous les noms d'instance localdb

J'ai deux versions (2012, 2014) de SQL Server Express LocalDB installées sur mon système.

Comment trouver tous les noms d'instances LocalDB existants?

J'ai trouvé un moyen de le faire en utilisant la ligne de commande comme mentionné dans la section des réponses.

Y a-t-il un moyen meilleur et facile de le faire?

32
vineel

J'ai trouvé l'utilitaire SqlLocalDB qui doit être exécuté en ligne de commande.

SqlLocalDB peut être trouvé dans

C:\Program Files\Microsoft SQL Server\110\Tools\Binn

ou

C:\Program Files\Microsoft SQL Server\120\Tools\Binn

Pour obtenir tous les noms d'instance LocalDB existants, utilisez:

SqlLocalDB.exe i

 info|i
  Lists all existing LocalDB instances owned by the current user
  and all shared LocalDB instances.

Pour obtenir des informations détaillées sur une instance spécifique de LocalDB:

SqlLocalDB.exe i "MSSQLLocalDB"

info|i "instance name"
  Prints the information about the specified LocalDB instance.
50
vineel

Si vous préférez utiliser l'interface utilisateur (risque de ne plus fonctionner avec SSMS2019)

Ouvrez simplement votre SSMS et connectez-vous à (LocalDB)\MSSQLLocalDB.
Maintenant, vous verrez toutes vos instances LocalDB .

Cela fonctionne au moins avec SS2016.

enter image description here

13
Legends

Dans Visual Studio 2017, l'explorateur d'objets SQL Server vous montrera toutes les instances LocalDb.

5
Ricky Keane

Voici la méthode que j'utilise pour obtenir toutes les instances de la ligne de commande -

    internal static List<string> GetLocalDBInstances()
    {
        // Start the child process.
        Process p = new Process();
        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/C sqllocaldb info";
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        string sOutput = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        //If LocalDb is not installed then it will return that 'sqllocaldb' is not recognized as an internal or external command operable program or batch file.
        if (sOutput == null || sOutput.Trim().Length == 0 || sOutput.Contains("not recognized"))
            return null;
        string[] instances = sOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        List<string> lstInstances = new List<string>();
        foreach (var item in instances)
        {
            if (item.Trim().Length > 0)
                lstInstances.Add(item);
        }
        return lstInstances;
    }
5
Ram