A company's data grabbing program has a large amount of data. The idatareader's read method is used for data processing,
During the test, I want to run part of the data and jump out of the loop, that is, break; then close datareader, but execute datareader. when the close () method is used, the "timeout exception" error occurs. Check the descriptions of the close method in msdn as follows:
When you use sqldatareader to use the associated sqlconnection for any other purpose, you must explicitly call the close method.
The close method fills in the output parameter values, return values, and recordsaffected, increasing the time used to close sqldatareader for processing large or complex queries. If the return value is not important to the number of records affected by the query, you can call the Cancel Method of the associated sqlcommand object before calling the close method to reduce the time required to disable sqldatareader.
The cancel method that previously executed the command can solve this problem.
1 Public void testdatareader () 2 {3 using (idatareader reader = cmd. executereader (commandbehavior. closeconnection) 4 {5 try 6 {7 while (reader. read () 8 {9 // process data 10 // break; 11 // process data 12} 13} 14 finally15 {16 cmd. cancel (); 17 reader. close (); 18} 19} 20}