2016-03-22 14 views
17

Ho due versioni (2012, 2014) di SQL Server Express LocalDB installate nel mio sistema.SQL Server: come trovare tutti i nomi di istanza localdb

Come posso trovare tutti i nomi di istanze LocalDB esistenti?

Ho trovato un modo per farlo utilizzando la riga di comando come indicato nella sezione risposte.

C'è un modo migliore e facile per farlo?

risposta

28

Ho trovato l'utilità SqlLocalDB che deve essere eseguita sulla riga di comando.

SqlLocalDB può essere trovato in

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

o

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

Per ottenere tutti LocalDB nomi di istanza esistenti, uso:

SqlLocalDB.exe i 

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

Per ottenere informazioni dettagliate su una specifica LocalDB un'istanza :

SqlLocalDB.exe i "MSSQLLocalDB" 

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

Ecco il metodo che sto usando per ottenere tutte le istanze da linea di comando -

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; 
    } 
0

In Visual Studio 2017 di SQL Server oggetto Explorer vi mostrerà tutte le istanze LocalDB

0

Se preferisci la modalità interfaccia utente, basta connettersi al tuo SSMS come questo e vedrai tutte le tue istanze LocalDB. Questo funziona almeno con SS2016, non può garantire questo per le versioni precedenti.

enter image description here

Problemi correlati