简体   繁体   English

C#:DBConnection.Open()超时太长

[英]C# : DBConnection.Open() timeout is too long

I'm trying to connect to a server that the user inputs. 我正在尝试连接到用户输入的服务器。 When the server doesn't exist, I'd like to give a quick feedback to the end-user so he can correct what he's typed. 当服务器不存在时,我想向最终用户提供快速反馈,以便他可以更正他输入的内容。

Is there any way to test if a server exists before trying to connect ? 在尝试连接之前有没有办法测试服务器是否存在?

Thanks 谢谢

ConnectionTimeout property in DbConnection does not have setter. DbConnection中的ConnectionTimeout属性没有setter。 It waits for 15 seconds. 它等了15秒。 You need to specify the timeout in your connection string. 您需要在连接字符串中指定超时。 "Connection Timeout" in the connection string is in seconds. 连接字符串中的“连接超时”以秒为单位。

string connStr = "Data Source=(local);Initial Catalog=AdventureWorks;" +
                 "Integrated Security=SSPI;Connection Timeout=5";

Does your DBConnection have a ConnectionTimeout property? 您的DBConnection是否具有ConnectionTimeout属性? Try setting this to a small number. 尝试将此设置为较小的数字。

You can use SQL Server Management Objects (SMO) to get a list of SQL Servers. 您可以使用SQL Server管理对象 (SMO)来获取SQL Server列表。 There is a sample of how to get a list of servers here . 还有就是如何让服务器列表的样品在这里 The enumeration of the servers can take a while but you could always start it in the background when the application loads so that it is ready when the user needs to select a server. 服务器的枚举可能需要一段时间,但是当应用程序加载时,您总是可以在后台启动它,以便在用户需要选择服务器时就绪。

您始终可以ping服务器或检查给定端口是否已打开]但这并不能保证此服务器在连接到真实之前不会脱机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM