当前位置:Gxlcms > 数据库问题 > MySqlConnection 并发连接的问题

MySqlConnection 并发连接的问题

时间:2021-07-01 10:21:17 帮助过:2人阅读

 也就是说同一时刻只允许有一个连接被打开读取数据。

 stackoverflow 上的分析如下:

     You are using the same connection for the DataReader and the ExecuteNonQuery. This is not supported, according to MSDN:

Note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You cannot execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.

 这下就明白了,我们在创建连接的时候必须每次new出来一个连接对象。改造一下代码:

 

public class DBConnection
    {
        public static MySqlConnection Conn
        {
            get
            {
                MySqlConnection connection = new MySqlConnection(ConstValue.DBConnectionString);
                return connection;
            }
        }

        public static MySqlConnection CreateConnection()
        {
            return Conn;
        }
    }

这样就解决了并发访问的时候出现的问题了。

踩过的坑,纪录下来就是成长。

 

MySqlConnection 并发连接的问题

标签:

人气教程排行