Using MySQLConnection in C# does not close properly

后端 未结 4 667
我在风中等你
我在风中等你 2020-12-01 18:15

Final solution:

The connection was added to the connection pool. So I closed it, but it still remained physically open. With the ConnectionString Parameter \"Pooli

4条回答
  •  执笔经年
    2020-12-01 18:57

    Have a look at using something like this:

    private static IEnumerable SqlRetrieve(
        string ConnectionString, 
        string StoredProcName,
        Action AddParameters)
    {
        using (var cn = new SqlConnection(ConnectionString))
        using (var cmd = new SqlCommand(StoredProcName, cn))
        {
            cn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
    
            if (AddParameters != null)
            {
                AddParameters(cmd);
            }
    
            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (rdr.Read())
                    yield return rdr;
            }
        }
    }
    

提交回复
热议问题