如何在不打开数据库的情况下测试SqlServer连接

发布于 2024-07-06 14:42:30 字数 249 浏览 6 评论 0原文

标题基本概括了所有内容。 我想创建一个 SqlConnection,然后在不打开数据库的情况下检查该连接,因为那时我还不知道将连接到哪里。 可以这样做吗? SqlConnection 类有一个“Open”成员,它尝试打开您在 Database 属性中设置的数据库,如果您没有设置,SqlServer 会尝试使用主数据库。 问题是我尝试连接的用户(MACHINE\ASPNET)可以访问某些数据库(我还不知道)而不是主数据库。

问候, 塞巴

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你好,陌生人 2024-07-13 14:42:31

连接到临时数据库。 每个人都可以访问 tempdb,因此您将能够验证自己的访问权限。 稍后当您知道实际的数据库时,您可以更改此属性以连接到您想要的数据库。

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.

爱她像谁 2024-07-13 14:42:31

我不确定这是否是您所需要的。

检查用户是否有权访问 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]

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]
旧情别恋 2024-07-13 14:42:31

如果您只需要知道服务是否处于活动状态,您可以尝试通过套接字连接到端口,以查看它是否打开

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

奢欲 2024-07-13 14:42:31

只是好奇...如果您不知道需要连接的精确数据库,您将能够验证哪些信息? “真实”数据库可能出现的许多问题是无法通过这种测试连接进行测试的,例如连接性或安全性。

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.

她如夕阳 2024-07-13 14:42:31

我不知道你是否得到了答案,但因为我们都在这里寻找答案,所以我希望这就是你正在寻找的

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文