我想了解連接超時和命令超時之間的關係,以及如何影響其他人。請考慮這個代碼作爲例子。SqlDataReader連接超時與命令超時
// declare the SqlDataReader, which is used in
// both the try block and the finally block
SqlDataReader rdr = null;
// create a connection object
SqlConnection conn = new SqlConnection("someconnstr");
// create a command object
SqlCommand cmd = new SqlCommand("select * from dbo.mytable", conn);
try
{
// open the connection
conn.Open();
// 1. get an instance of the SqlDataReader
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
// get the results of each column
Guid Id = (Guid)rdr["Id"];
string displayName = (string)rdr["Name"];
// print out the results
Console.WriteLine("{0}, {1}", Id, displayName);
}
Console.WriteLine("Reading done");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
根據MSDN link,命令超時是所有讀取的累積超時。這意味着如果您再次調用Read(),則會有30秒的時間完成。我想以這樣的方式設置超時時間,以便可以對所有記錄施加最大超時時間。
連接超時是否是一件好事?在給定的例子中,如果我將連接超時設置爲120秒,並且while循環在120秒內未完成,是否會引發超時錯誤?
這個問題與Stackoverflow question有關。
這應該回答你的問題:[SqlCommand.CommandTimeout和SqlConnection.ConnectionTimeout之間的區別是什麼?](http://stackoverflow.com/questions/847264/what-is-the-difference-between-sqlcommand-commandtimeout -and-sqlconnection-conne) –
我的問題有點不同。 SqlConnection.ConnectionTimeout用於打開連接的時間限制。我想強制讀取所有記錄的最長時間(while循環可以佔用的總時間)。 – murtazat
什麼是你強加最大時間的理由?是什麼讓你得出結論:你需要實現最大超時? –