如何在c#中创建与Mysql的全新连接?
在存储过程内部,我创建了一个临时表来存储结果,我知道如果在一个连接中对该存储过程有多个调用(因为它只使用一个临时表名=>不知道哪个属于哪个请求),则会发生冲突 如您所见,在同一连接中,每次调用此存储时,都会再添加一行 在我的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
public int CallStoreFunction()
{
return result = dbcontext.Database.SqlQuery<int>("CALL store_proc()");
}
提前谢谢 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),则通常会从池中返回相同的连接<代码>临时表应该是每个会话的表,因此即使使用连接池,它们也应该是唯一的。