c# - 连接关闭后,MySql 临时表仍然存在(来自.NET)

标签 c# mysql .net

我正在 MySql 中创建一个临时表,并希望在断开连接后它不会存在。我已经从代码中删除了所有内容,只留下了这个:

        //create the first connection
        var connection = new MySqlConnection(ConnectionString);
        connection.Open();

        //create a temp table
        var cmd = connection.CreateCommand();
        cmd.CommandText = "CREATE TEMPORARY TABLE SomeDummyTable(Column1 float)";
        cmd.ExecuteNonQuery();

        //read the new temp table to proove it's created
        cmd = connection.CreateCommand();
        cmd.CommandText = "select * from SomeDummyTable";
        cmd.ExecuteNonQuery();

        //close and dispose of the first connection
        connection.Close();
        connection.Dispose();

        //create new connection
        MySqlConnection connection2 = new MySqlConnection(ConnectionString);
        connection2.Open();
        var cmd2 = connection2.CreateCommand();

        cmd2.CommandText = "select * from SomeDummyTable";
        //this does not throw an exception, even though SomeTable is 
        //a temp table from the previous connection
        cmd2.ExecuteNonQuery();

因此,我使用一个连接创建一个临时表,然后关闭该连接并打开另一个连接。我可以使用第二个连接查看临时表。是什么赋予了?我错过了什么?

此外:如果我关闭应用程序并重新打开它,则临时表不存在。可能是某种连接池之类的吗?

最佳答案

这似乎是 MySQL 连接池的错误,或者可能是您正在使用的特定提供程序的错误。根据我在粗略搜索中找到的内容,当创建临时表的连接关闭时,临时表应该自动删除。我期望连接池会改变这种行为。

然后我想到了两种解决方法:

  1. 使用完临时表后,显式删除它。
  2. 通过将 Pooling=False; 添加到连接字符串来禁用连接池。

This MySQL bug report似乎很相关。显然其他连接属性没有被重置。也许临时表没有立即删除(或者以其他方式使后续连接无法访问)是同一问题的一部分。

You should report the bug.我搜索了“临时表连接池”的错误,但没有找到任何明确涵盖您的场景的错误。

关于c# - 连接关闭后,MySql 临时表仍然存在(来自.NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768917/

相关文章:

c# - 在C#中使用套接字编程接收消息不起作用

php - html图片自动调整div的大小

javascript - 如何获取剑道上下文菜单中的当前行?

c# - 命名空间 'class' 中的元素 'urn:nhibernate-mapping-2.2' 具有无效的子元素 'property'

c# - 如何对 Controller 中的 View 进行编程更改出现在 Storyboard 中

c# - GlobalConfiguration.Configure(WebApiConfig.Register) 挂起

php - SugarCRM 数据库变量不允许我从我自己的属性中调用 quote() 函数

php - 在一个查询中获取作者用户名和上次更新用户名

.net - Servicestack.Redis是否支持启用集群的redis?

c# - 如何使用 Not In datatable.select