This article introduces a detailed example of DataReader not being able to use using. It is shared with you, and has the following:
public static MySqlDataReader ExecuteMySqlReader(string sqlStr) { MySqlConnection conn = new MySqlConnection(MyConString); MySqlCommand cmd = new MySqlCommand(sqlStr, conn); try { (); // When executing the CloseConnection command, if the associated DataReader object is closed, the associated Connection object will also be closed//Using using(conn) will report an error, because the connection will be closed after executing the command. When other codes call the DataReader object, the connection has been closed.MySqlDataReader dr = (); return dr; } catch (Exception exp) { throw new Exception(); } }
PS: MySqlDataReader is used in Using
Conclusion: When the DataReader is placed in the Using method, the resource will be automatically released. If an exception occurs in the middle, the occupied resource will also be released.
Testing process: Since the entire process is not recorded here, I just want to explain the results in a general way. Interested children's shoes can test them by themselves.
First, the normal processing process:
MySqlDataReader dr = (, , sqlStr, parameters) while (()) { (dr["KeyWord"].ToString()); } ()
This seems to be fine, but in the test, if an exception occurs "(dr["KeyWord"].ToString());", the program will be tuned to the exception handling module, which will cause the following close method to not be executed, resulting in the continuous accumulation of the database connection number. When the maximum value is reached, the problem will be revealed.
The following first method of handling uses exception processing:
MySqlDataReader dr = (, , sqlStr, parameters); try{ while (()) { (dr["KeyWord"].ToString()); } } catch(){ } finnally{ (); }
There is no doubt that this method is easy to think of.
The second processing method is to use the using method here for processing. But based on theoretical knowledge, I think this is OK. However, the real program operation environment cannot be guided by theoretical knowledge sometimes. Now that the program is here, there is a very suitable testing environment, why not test it yourself? So a process occurred:
I am using a program written by MySql database and C#.
First, add some basic knowledge:
1. Using definition scope:Release resources instantly, and at the end of the scope. When a class instance is used in a certain code segment, no matter what the reason is, as long as the code segment is left, the Dispose method of this class instance will be automatically called to release the resource.
An exception is raised at the end of the using statement or in the middle of the using statement and the control leaves the statement block, that is, the Dispose method that triggers the instance to release the resource.
Then check the implementation of MySqlDataReader:
public sealed class MySqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord{...}
It is indeed inherited from the IDisposable method, which should be theoretically correct.
2. MySql view the number of connections:
Command: show processlist; If it is a root account, you can see the current connection of all users. If it is another ordinary account, you can only see the connection you occupy.
show processlist; only list the first 100 items. If you want to list them all, please use show full processlist;
With these two theoretical knowledge, the following tests are much easier:
1. Do not use using or close the connection:
MySqlDataReader dr = (, , sqlStr, parameters); while (()) { (dr["KeyWord"].ToString()); }
Test, the number of connections continues to increase.
2. If not used, use the shutdown operation:
MySqlDataReader dr = (, , sqlStr, parameters); while (()) { (dr["KeyWord"].ToString()); } ()
Test, the number of connections does not change.
3. Do not use Using, use a shutdown operation, and create an exception during the middle execution process:
MySqlDataReader dr = (, , sqlStr, parameters); while (()) { (dr["Keyord"].ToString()); } ()
Test, the number of connections continues to increase.
4. Use Using, no abnormalities:
Using(MySqlDataReader dr = (, , sqlStr, parameters)) { while (()) { (dr["KeyWord"].ToString()); } }
Test, the number of connections has not increased.
5. Use Using to create an exception:
Using(MySqlDataReader dr = (, , sqlStr, parameters)) { while (()) { (dr["Keyord"].ToString()); } }
Test, the number of connections has not increased.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.