How to test a SqlServer connection without opening a database

The name says a lot about everything. I want to create a SqlConnection, and then test this connection without opening the database, because at this point I still do not know what to connect to. Can this be done? The SqlConnection class has an Open element, which tries to open the database that you installed in the Database property, and if you did not install it, SqlServer tries with master db. The fact is that the user I'm trying to connect to (MACHINE \ ASPNET) has access to some databases (which I don’t know yet), and not to master db.

Regards, Seba

+5
source share
5

temp db. accecss tempdb, . , , , db.

+10

, , .

, Sql Server 2005

SELECT HAS_DBACCESS('Northwind');

HAS_DBACCESS , (BOL).

,

SELECT [Name] as DatabaseName from master.dbo.sysdatabases
WHERE ISNULL(HAS_DBACCESS ([Name]),0)=1
ORDER BY [Name]
+2

, , , ,

+1
source

Just curious ... What information can you check if you do not know the exact database you need to connect to? Many things that might go wrong with a “real” database would be unchecked from these kinds of test connections, such as connection or security.

0
source

I don't know if you got your answers, but since we all look at the answers here. Hope this is what you were looking for.

dim con as new sqlconnection
con.connectionstring="<<put your conn string here>>"
'try...catch block fires exception if the con is not successfully opened
try
con.open()
catch ex as exception
msgbox ex.message
end try
-1
source

All Articles