The read attempt is invalid when the reader is disabled.

Source: Internet
Author: User

A problem recently encountered is the problem of datareader transmission in a multi-layer architecture.

 

The result shows that "the Read Attempt is invalid when the reader is disabled .", I checked it online for two days because most Code does not involve the problem of datareader transfer. At one time, I thought datareader could not be passed. I called the code in sqlhelper, so I never doubted the underlying code. The original code is as follows:

 

Public static dbdatareader executereader (string SQL)
{
Using (dbconnection conn = getconncetion ())
{
Dbcommand cmd = getcommand (conn );
Dbdatareader myreader;
Try
{
Conn. open ();
Setcommand (CMD, SQL, commandtype. Text, commandtime, null );
Myreader = cmd. executereader (commandbehavior. closeconnection );
Return myreader;
}
Catch (dbexception ex)
{
Throw new exception (ex. Message );
}

Finally
{
Cmd. Dispose ();
Conn. Close ();
}
}
}

 

Finally, I found a post today. The original using problem was discovered. In addition, finally automatically disables the conn, regardless of whether the reader transmits the conn. Later, I changed the code to the following and finally ran it!

 

Public static dbdatareader executereader (string SQL)
{
// Using (dbconnection conn = getconncetion ())
//{
Dbconnection conn = getconncetion ();
Dbcommand cmd = getcommand (conn );
Dbdatareader myreader;
Try
{
Conn. open ();
Setcommand (CMD, SQL, commandtype. Text, commandtime, null );
Myreader = cmd. executereader (commandbehavior. closeconnection );
Return myreader;
}
Catch (dbexception ex)
{
Throw new exception (ex. Message );
}

// Finally
//{
// Cmd. Dispose ();
// Conn. Close ();
//}
//}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.