Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中创建与Mysql的全新连接?_C#_Mysql_Entity Framework_Stored Procedures_Dbcontext - Fatal编程技术网

如何在c#中创建与Mysql的全新连接?

如何在c#中创建与Mysql的全新连接?,c#,mysql,entity-framework,stored-procedures,dbcontext,C#,Mysql,Entity Framework,Stored Procedures,Dbcontext,在存储过程内部,我创建了一个临时表来存储结果,我知道如果在一个连接中对该存储过程有多个调用(因为它只使用一个临时表名=>不知道哪个属于哪个请求),则会发生冲突 如您所见,在同一连接中,每次调用此存储时,都会再添加一行 在我的C#项目中,我测试了几种方法来建立一个新的MySQL连接来调用存储过程 创建新的dbcontext(在实体框架中),然后使用 public int CallStoreFunction() { return result = dbcontext.Database.SqlQ

在存储过程内部,我创建了一个临时表来存储结果,我知道如果在一个连接中对该存储过程多个调用(因为它只使用一个临时表名=>不知道哪个属于哪个请求),则会发生冲突

如您所见,在同一连接中,每次调用此存储时,都会再添加一行

在我的C#项目中,我测试了几种方法来建立一个新的MySQL连接来调用存储过程

  • 创建新的dbcontext(在实体框架中),然后使用

    public int CallStoreFunction()
    {
       return result = dbcontext.Database.SqlQuery<int>("CALL store_proc()");
    }
    
  • 对于每种方式,我调用这个函数四次,结果毕竟是4次(这意味着每次调用存储仍然处于相同的连接中)

    看起来C#只是创建了一个新的会话/事务,而不是一个新的连接,无论我使用哪种方法。我想知道的是,每次我向这个商店发出请求时,它是一种创建到DB的完全不同的连接的方法吗?
    提前谢谢

    ADO.NET管理连接池中的连接。当您调用Open连接时,它将在池中查找可用的连接。如果可用,将返回(不要创建新连接)。当您调用Close时,它将返回到处于活动状态的池中(准备好重新使用)

    因此,您的代码仍然使用相同的连接和相同的临时表。如果要创建完全连接,应先清除池。ADO.NET支持ClearAllPool和ClearPool,对于MySQL,它看起来像:

    MySql.Data.MySqlClient.MySqlConnection.ClearPool(connection);
    
    例如:

            var result = 0;
    
            var connectionString = ConfigurationManager.ConnectionStrings["your_connection"].ConnectionString;
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var command = new MySqlCommand("CALL new_procedure()", connection);
                result = int.Parse(command.ExecuteScalar().ToString());
                MySql.Data.MySqlClient.MySqlConnection.ClearPool(connection);
                connection.Close();                 
            }
    
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var command = new MySqlCommand("CALL new_procedure()", connection);
                result = int.Parse(command.ExecuteScalar().ToString());
                connection.Close();                
            }
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var command = new MySqlCommand("CALL new_procedure()", connection);
                result = int.Parse(command.ExecuteScalar().ToString());
                connection.Close();                
            }    
    
    结果=1,结果=2


    参考:

    ADO.NET管理连接池中的连接。当您调用Open连接时,它将在池中查找可用的连接。如果可用,将返回(不要创建新连接)。当您调用Close时,它将返回到处于活动状态的池中(准备好重新使用)

    因此,您的代码仍然使用相同的连接和相同的临时表。如果要创建完全连接,应先清除池。ADO.NET支持ClearAllPool和ClearPool,对于MySQL,它看起来像:

    MySql.Data.MySqlClient.MySqlConnection.ClearPool(connection);
    
    例如:

            var result = 0;
    
            var connectionString = ConfigurationManager.ConnectionStrings["your_connection"].ConnectionString;
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var command = new MySqlCommand("CALL new_procedure()", connection);
                result = int.Parse(command.ExecuteScalar().ToString());
                MySql.Data.MySqlClient.MySqlConnection.ClearPool(connection);
                connection.Close();                 
            }
    
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var command = new MySqlCommand("CALL new_procedure()", connection);
                result = int.Parse(command.ExecuteScalar().ToString());
                connection.Close();                
            }
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var command = new MySqlCommand("CALL new_procedure()", connection);
                result = int.Parse(command.ExecuteScalar().ToString());
                connection.Close();                
            }    
    
    结果=1,结果=2


    参考:

    创建临时表时,您使用的是语法,对吗?EF和ADO都使用连接池,因此如果连接已被释放(您正在使用Close+Dispose),则通常会从池中返回相同的连接<代码>临时表应该是每个会话的表,因此即使使用连接池,它们也应该是唯一的。当您创建临时表时,您使用的是语法,对吗?EF和ADO都使用连接池,因此如果连接已被释放(您正在使用Close+Dispose),则通常会从池中返回相同的连接<代码>临时表应该是每个会话的表,因此即使使用连接池,它们也应该是唯一的。