2016-03-22 17 views
17

Mam dwie wersje (2012, 2014) SQL Server Express LocalDB zainstalowane w moim systemie.Serwer SQL: znajdowanie wszystkich nazw instancji localdb

Jak mogę znaleźć wszystkie istniejące nazwy instancji LocalDB?

Znalazłem sposób, aby to zrobić za pomocą wiersza poleceń, jak wspomniano w sekcji odpowiedzi.

Czy istnieje lepszy i łatwiejszy sposób na zrobienie tego?

Odpowiedz

28

Znalazłem narzędzie SqlLocalDB, które musi być uruchomione w wierszu poleceń.

SqlLocalDB można znaleźć w

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

lub

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

Aby uzyskać wszystkie istniejące LocalDB nazw instancji, zastosowanie:

SqlLocalDB.exe i 

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

Aby uzyskać szczegółowe informacje na temat konkretnego LocalDB instancji :

SqlLocalDB.exe i "MSSQLLocalDB" 

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

Oto metoda używam, aby uzyskać wszystkie instancje z linii poleceń -

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

W Visual Studio 2017 SQL Server Object Explorer pokaże wszystkie instancje LocalDb

0

Jeśli wolisz sposób interfejsu użytkownika, po prostu połącz się ze swoim SSMS w ten sposób, a zobaczysz wszystkie swoje LocalDB-Instances. Działa to przynajmniej z SS2016, nie może zagwarantować tego dla wcześniejszych wersji.

enter image description here

Powiązane problemy