积极答复者
C ++ ADO 连接数据库SQL 设置连接超时问题。

问题
-
// 定义数据库操作指针 _ConnectionPtr m_pConnection; // 设置连接超时时间为3秒。 m_pConnection->ConnectionTimeout = 3; m_pConnection->Open(bsConnect,"","",adModeUnknown);<br/>
应用上述代码进行数据库连接,但是发现如果目标数据库主机IP地址不存在的情况下,连接超时将不起作用,还会默认的等待30秒左右,
仅有在IP地址存在或者本机网线断开的情况下设置才起作用,忘给予好的解决办法,让在连接数据库时,即使在连接字段的IP地址不存在的情况下也能进行超时设定。
May the force be with me!
答案
-
你好,
ConnectionTimeout属性本身就是判断是否连接超时。如果需要判断目标主机是否存在,我建议您可以考虑在链接数据库前先进行一次判断。如目标主机存在则链接。你可以使用gethostbyname方法来判断。您可以参照这个网页中的例子来修改您的代码:http://msdn.microsoft.com/en-us/library/ms738524(VS.85).aspx
此外这是在MSDN上关于ConnectionTimeout的页面:http://msdn.microsoft.com/en-us/library/ms676718(v=VS.85).aspx.
在这个页面中有关于数据库链接的一个例子:http://msdn.microsoft.com/en-us/library/ms678277(v=VS.85).aspx
希望我的回答对你的问题有所帮助
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Rob Pan 2011年7月20日 10:05
-
网卡内有一个连接超时的设置,这个设置依赖于硬件的固件。你可以用telnet IP 1433 判断你碰到的超时是来自你的代码还是来自硬件。
建议使用单独线程定时检查连接是否断开,然后在连接之前检查线程内上次检查的结果。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Rob Pan 2011年7月20日 10:05
全部回复
-
你好,
ConnectionTimeout属性本身就是判断是否连接超时。如果需要判断目标主机是否存在,我建议您可以考虑在链接数据库前先进行一次判断。如目标主机存在则链接。你可以使用gethostbyname方法来判断。您可以参照这个网页中的例子来修改您的代码:http://msdn.microsoft.com/en-us/library/ms738524(VS.85).aspx
此外这是在MSDN上关于ConnectionTimeout的页面:http://msdn.microsoft.com/en-us/library/ms676718(v=VS.85).aspx.
在这个页面中有关于数据库链接的一个例子:http://msdn.microsoft.com/en-us/library/ms678277(v=VS.85).aspx
希望我的回答对你的问题有所帮助
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Rob Pan 2011年7月20日 10:05
-
网卡内有一个连接超时的设置,这个设置依赖于硬件的固件。你可以用telnet IP 1433 判断你碰到的超时是来自你的代码还是来自硬件。
建议使用单独线程定时检查连接是否断开,然后在连接之前检查线程内上次检查的结果。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Rob Pan 2011年7月20日 10:05