开发者

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

I'm trying to connect to a server that the user inpu开发者_StackOverflowts. 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


Does your DBConnection have a ConnectionTimeout property? Try setting this to a small number.


ConnectionTimeout property in DbConnection does not have setter. It waits for 15 seconds. 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";


You can use SQL Server Management Objects (SMO) to get a list of SQL Servers. 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.


You can always ping the server or check if a given port is open] but this won't guarantee you that this server won't get offline just before connecting for real.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜