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.
标题基本概括了所有内容。我想创建一个SqlConnection然后检查该连接而不打开数据库,因为那时我还不知道我将连接到哪里。有可能吗? SqlConnection类有一个'Open'成员,它试图打开你在Database属性中设置的数据库,如果你没有设置它,SqlServer会尝试使用master db。问题是我正在尝试连接的用户(MACHINE \ ASPNET)可以访问某些数据库(我还不知道),而不是主数据库。
Regards, Seba
此致,塞巴
5 个解决方案
#1
10
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.
连接到临时数据库。每个人都加入了tempdb,因此您可以对自己进行身份验证以便访问。稍后当您知道实际数据库时,可以更改此属性以连接到所需的数据库。
#2
2
I am not sure if this is what you need.
我不确定这是否是你需要的。
Check if a user has access to a database in Sql Server 2005
检查用户是否可以访问Sql Server 2005中的数据库
SELECT HAS_DBACCESS('Northwind');
HAS_DBACCESS returns information about whether the user has access to the specified database (BOL).
HAS_DBACCESS返回有关用户是否有权访问指定数据库(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]
#3
1
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
如果您只需要知道服务是否处于活动状态,您可以尝试通过套接字连接到端口,以查看它是否处于打开状态
#4
0
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.
只是好奇......如果您不知道需要连接的精确数据库,您将能够验证哪些信息?许多可能出错的“真实”数据库在这种测试连接中是不可测试的,例如连接或安全性。
#5
-1
I don't know whether you got your answers but as we all look here for answers I 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
10
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.
连接到临时数据库。每个人都加入了tempdb,因此您可以对自己进行身份验证以便访问。稍后当您知道实际数据库时,可以更改此属性以连接到所需的数据库。
#2
2
I am not sure if this is what you need.
我不确定这是否是你需要的。
Check if a user has access to a database in Sql Server 2005
检查用户是否可以访问Sql Server 2005中的数据库
SELECT HAS_DBACCESS('Northwind');
HAS_DBACCESS returns information about whether the user has access to the specified database (BOL).
HAS_DBACCESS返回有关用户是否有权访问指定数据库(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]
#3
1
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
如果您只需要知道服务是否处于活动状态,您可以尝试通过套接字连接到端口,以查看它是否处于打开状态
#4
0
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.
只是好奇......如果您不知道需要连接的精确数据库,您将能够验证哪些信息?许多可能出错的“真实”数据库在这种测试连接中是不可测试的,例如连接或安全性。
#5
-1
I don't know whether you got your answers but as we all look here for answers I 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