When I run our service stand alone or when I run the same service as windows service logged on as the current Windows user then I cannot see the databases on LocalDb installed by the same Windows service when it was launched logged on as LocalSystem. And the other way around, the same code that is supposed to get the list of installed on LocalDb server databases, doesn't return those installed databases, if the latter were installed on the LocalDb server by an application started as current Windows user and if the current service is running logged on as LocalSystem and we try to get the databases on the server.
I tried getting the databases on the LocalDb server two different ways and the list is the same for both ways.
// First way executing the following sql query from my C# code:
string query = $"SELECT name FROM sys.databases WHERE database_id > 4";
// Second way using the Server class as below (which probably under the hood does the same as the first way):
new Microsoft.SqlServer.Management.Smo.Server("(localdb)\\v11.0").Databases
I know there are permission and authentication differences whether we connect to LocalDb server from the code that runs in the Windows Service and the latter is logged on as current user or LocalSystem, that's why the databases on the server might not be visible from one case to another.
My question is how can I get the complete list of databases installed by any users any time?