C#命令超时问题中的SQL连接 [英] sql connection in c# command timeout issue

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

问题描述

我正在从c#查询一个表,并显式设置CommandTimeout = 0,但仍然遇到以下错误.我在相同环境中的其他应用程序中使用过类似的代码,并且sql connectionstring也相同,并且从未遇到任何连接超时.

Hi, I am querying a table from c# and explicitly setting CommandTimeout = 0 and still run into the below error. I have used similar code in other applications in the same environment and sql connectionstring also same and never ran into any connection timeout.

错误: :System.Data.SqlClient.SqlException(0x80131904):执行超时已过期.  操作完成之前已经过了超时时间,或者服务器没有响应.

Error: System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

代码:

Code:

static void Main(string[] args)
{
 string cString = @"Data source = server;Integrated Security = true;Initial Catalog = database";

 using(SqlConnection sc = new SqlConnection(cString))
 {
  try
  {
   SqlCommand cmd = new SqlCommand("select * from table", sc);
   cmd.CommandTimeout = 0;
   Console.WriteLine("CommandTimeout: {0}", cmd.CommandTimeout);
   sc.Open();
   SqlDataReader r = cmd.ExecuteReader();

   while (r.Read())
   Console.WriteLine("{0}: {1}", r[0].ToString(), r[1].ToString());
   sc.Close();
  } 
  catch (SqlException se)  
  {
   Console.WriteLine(se);
  }
 }
}

谢谢.

SQLEnthusiast

SQLEnthusiast

推荐答案

哪个代码行是问题所在?

Which line of code is the issue on?

数据库在本地还是在服务器上?

Is the database local or on a server?


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

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