views:

6140

answers:

6

The title pretty much says it all. I want to create a SqlConnection and then check that connection without opening a database, cause at that point I don't know yet where will I connect to. Is it possible to do that? The SqlConnection class has a 'Open' member which tries to open the database you'd set in the Database property, and if you didn't set one, SqlServer tries with the master db. The thing is the user I'm trying to connect with (MACHINE\ASPNET) has access to some databases (which I don't know yet) and not the master db.

Regards, Seba

A: 

Just curious... What information will you be able to verify if you don't know the precise database you need to connect to? Many things that could go wrong with the "real" database would be untestable from this sort of test connection, such as connectivity or security.

Chris Farmer
well... I just wanna know if the user can authenticate against the SqlServer, thus, it is a valid server and a valid login in that server...I guess the answer is NO you can't
sebastian
+1  A: 

I am not sure if this is what you need.

Check if a user has access to a database in Sql Server 2005

SELECT HAS_DBACCESS('Northwind');

HAS_DBACCESS returns information about whether the user has access to the specified database (BOL).

Find all databases that the current user has access to

SELECT [Name] as DatabaseName from master.dbo.sysdatabases
WHERE ISNULL(HAS_DBACCESS ([Name]),0)=1
ORDER BY [Name]
Gulzar
that's not what I needed but thanks
sebastian
+6  A: 

Connect to temp db. Everybody has accecss to tempdb so you will be able to authenticate yourself for access. Later when you know the actual database , you can change this property to connect to the db you want.

Learning
A: 

The login I created does not have a user mapping against tempdb, but I'll try that... I'll let you guys know

sebastian
A: 

If you need to know only if the service is active, you could try to connet via a socket to the port, to see if it is open

Lorenzo Boccaccia
A: 

It wroked! thanks Learning :D

sebastian
Glad to be of help! :)
Learning