In Visual Basic 2008, my application is connecting to one of several possible (identically structured) SQL Server databases on a server. The user of the application may have permission to use only some of the databases. What's the most efficient way to determine whether the user can access a particular database? Currently, I'm simply enumerating all the databases on the server
select name from master..sysdatabases where name <> 'master' and name <>'tempdb' and name<>'model' and name<>'msdb'
then trying a SELECT on one of the tables in each database. If I don't have permission to use the database, I get an exception and move on to the next.
But it's well known that exceptions are very slow. That means that the enumeration of available databases is slow, as well. It seems to me there ought to be a better method to check permissions. Suggestions?