的CommandTimeout不工作 [英] CommandTimeout not working

查看:353
本文介绍了的CommandTimeout不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变超时一个SqlCommand查询,在测试我对一个给定的连接字符串连接的方法。在code是类似这样的:

I am trying to change the timeout for a SqlCommand query, in a method that tests my connection for a given connection string. The code is similar to this:

using (SqlConnection connection = new SqlConnection(connectionString))
    {
      SqlCommand cmd = new SqlCommand("SELECT ...", connection);
      cmd.CommandTimeout = 10;
      connection.Open();
      SqlDataReader reader = cmd.ExecuteReader();
      ...
      connection.Close();
    }

我想有一个短超时这里,因为我只是想测试一下这个连接字符串是好的。 但是,无论是什么号码,我在的CommandTimeout设置(我试过0,1,2,4,10,30,60,120),我对一个虚拟连接字符串获得实时大约总是大致相同(总运行时间15秒)。

I would like to have a short timeout here, since I just want to test if this connection string is okay. But, no matter what number I set on CommandTimeout (I tried 0, 1, 2, 4, 10, 30, 60, 120), my real time obtained for a dummy connection string is always about the same (total running time of about 15 seconds).

所以,在我看来,我上的CommandTimeout设置的值被忽略的某些原因。

So, seems to me that the value I set on CommandTimeout is being ignored for some reason.

任何想法,为什么?

推荐答案

我想你混淆,究竟 SqlCommand.CommandTimeout 是。按照这个MSDN参考

获取或设置等待时间终止之前执行命令的尝试并产生一个错误。

Gets or sets the wait time before terminating the attempt to execute a command and generating an error.

在你的情况,你正在执行一个DataReader,并通过查询步进(不管它可能是)。它以最少的时间为每个阅读()这就是为什么你会不会打你的超时。

In your case, you are executing a DataReader and stepping through your query (whatever it may be). It is taking minimal time for each Read() which is why you wouldn't be hitting your timeout.

修改

如果您使用的是错误的连接字符串,你的超时不会命令超时,但它会是连接时间。默认为15秒。这是超时是有效在您的情况。

If you are using a wrong connection string, your Timeout will not be the Command timeout, but it'll be the Connection time. This defaults to 15 seconds. That is the timeout that is effective in your situation.

您打算超时的方法调用在SQLConnection.open(),不是 SqlCommand.ExecuteReader()。因此, ConnectionTimeout 属性将是有效的超时值。

You're going to timeout on the method call SqlConnection.Open(), not SqlCommand.ExecuteReader(). Therefore the ConnectionTimeout property is going to be the effective timeout value.

<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout.aspx"相对=nofollow> SqlConnection.ConnectionTimeout物业MSDN参考

这篇关于的CommandTimeout不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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