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 ();
//}
//}
}