C#SqlServer连接超时问题 [英] C# SqlServer connection timeout problem

查看:1385
本文介绍了C#SqlServer连接超时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有问题。当我连接到sqlserver数据库时,如果机器已关闭或机器没有sqlserver服务,我的progrem将阻塞大约30秒。它太长了,所以我改变了连接字符串,如:

Hello,

I have a problem. When I connect to a sqlserver database, if the machine has closed or the machine does not have a sqlserver service, my progrem will block about 30s. It's too long, so I change the connection string like:

SqlConnection con = new SqlConnection("server=someServer;uid=sa;pwd=removed;database=FileTransport;Connection Timeout=5;");



我设置命令timeout = 5,但没有效果......



有什么问题,以及如何解决它?

谢谢!


and I set the command timeout =5,but not effect......

What's the problem,and how to resolve it?
Thanks!

推荐答案

您正在思考/计划/指导基于不是快乐路径场景的场景。如果将超时减少到5,您是否考虑过应用程序中的正常工作流程会发生什么?可能有一些实例可能需要5秒以上才能从数据库中获取数据。这些场景怎么样?
You are thinking/planning/chaning based on a scenario which is not a 'Happy path' scenario. Have you given a thought on what would happen to your normal workflow in application if you reduce your timeout to 5? There might be instances that can take more than 5seconds to get back data from database. What about those scenarios?


尝试使用 con.ConnectionTimeout


SQL Server中有两个超时:

连接超时

命令超时



我猜你有一个长时间运行的查询导致延迟,尝试设置 SqlCommand.CommandTimeout

请参阅: SqlCommand.CommandTimeout属性(System.Data.SqlClient) [ ^ ]
There are two timeouts in SQL Server:
connection timeout
command timeout

I guess you have a long running query which causes the delay, try setting the SqlCommand.CommandTimeout.
See: SqlCommand.CommandTimeout Property (System.Data.SqlClient)[^]


这篇关于C#SqlServer连接超时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆