C#.net MySQL There is already a open DataReader associated with the This command raises the issue

Source: Internet
Author: User

"Reference"there is already a open DataReader associated with the This command raises the issue

I did not use DataReader in the statement, no hint of the same error, this DataReader hidden in where, I give you here, because I am not enough to study the depth, only know that there is a method of the call will generate DataReader, I think this is also the problem that most people have a headache.

When you use database updates or INSERT statements, you typically use the SqlCommand ExecuteNonQuery () method, and if you define a public database connection, this problem does not exist if you generate a new connection each time you query. However, the database occupies a much higher amount of resources, and after ExecuteNonQuery (), an empty DataReader object is generated internally, and the DataReader is released when the current database connection is closed. Therefore, when you use the Update method, it is recommended to use the Using keyword, which, as a statement, frees the objects within the scope that it defines.

1. The workaround is to add a parameter "multipleactiveresultsets" to ConnectionStringand set its value to true. X

server=192.168.9.24;database=flgl;user=sa;password=1231q2w34; Multipleactiveresultsets=true

2, the connection is not closed. The current state of the connection is connecting. √

using (Flglconn = new SqlConnection (FLGLCONNSTR))
{
SqlCommand cmd = new SqlCommand ();
DataSet ds = new DataSet ();
Cmd.commandtype = CommandType.StoredProcedure;
Cmd.commandtext = procname;
if (ParamArray! = null)
Cmd. Parameters.addrange (ParamArray);
Try
{
With SqlDataAdapter you don't have to write Conn.Open () and Conn.close (), which is superfluous.
Cmd. Connection = Flglconn;
SqlDataAdapter SDA = new SqlDataAdapter (cmd);
Sda. Fill (DS);
}
catch (Exception ex)
{
Log.writeerrorlog (ex. Message + ex. Source + "--The SQL statement is:" + procname);
}
Finally
{
Flglconn.close ();
}
return DS;
}

C#.net MySQL There is already a open DataReader associated with the This command raises the issue

Related Article

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.