martincostello/sqllocaldb

GetOrCreateInstance can fail with "The SQL LocalDB instance 'x' does not exist"

Closed this issue · 3 comments

Hi @martincostello,

Happily using this library - thanks for the work :)

I have found an edge case in the GetOrCreateInstance extension method. The problem is that the set of instances returned from GetInstances() are not necessarily valid when passed to GetInstance().

Strange I know, but I think GetInstances API just interrogates the registry (HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Server\UserInstances), but that list can include invalid entries. I've had this myself due to deletion of Windows accounts - you end up with invalid instances in the list that you can't remove with the tools, you have to hack the registry.

So basically you can't just check names against that list to see if it "exists" - I think the definitive "exists" is whether GetInstance throws. Not as efficient but more accurate. Yes it's a bit of a crappy API provided by MSFT, but it would be great if this library could hide that from users.

Thanks for the information @JackUkleja.

I think I recall seeing this myself before now a very long time ago, where data would get left in the file system and instances would become corrupted.

I'll take a look some time this week and see if there's anything I can do to improve the behaviour.

I've made some changes as part of #39 that should make this scenario a bit better, but there might still be some cases where the state gets corrupted somehow and the native API's results are misleading.

Resolved by #39.