这来自我的Windows窗体应用程序的“更新端口”功能,旨在切换数据库中的布尔值,以便在ESP8266上更新值。按下按钮直到ESP检测到更改,延迟时间约为6秒。我已经确定缓慢的部分是在尝试重新连接时,因为执行连接后执行响应按钮按下所需的所有3个请求的速度非常快。有没有办法可以减少连接期间的响应时间?
private void button1_Click(object sender, EventArgs e)
{
toggleState("1");
}
private void toggleState(string _utgang)
{
bool? state = cmd("SELECT Status FROM Traverskran WHERE Utgang = " + _utgang);
if (state != null)
{
state = !state;
cmd("UPDATE Traverskran SET Status = " + state.ToString() + " WHERE Utgang = " + _utgang);
}
updatePortStats();//Differentt function that does the same as cmd() but sends "SELECT * FROM Traverskran" and dumps it into a textBox
}
private bool? cmd(string query)
{
string connectionString = ("server=" + url + ";" + "username=" + username + ";" + "password=" + password + ";" + "database=" + databaseName + ";");
connection = new MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(query, connection);
command.CommandTimeout = 60;
try
{
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
bool? result = (reader.GetString(0) == "True");
connection.Close();
return result;
}
connection.Close();
return null;
}
catch (Exception e1)
{
connection.Close();
return null;
}
}
答案 0 :(得分:0)
不要为每次按下按钮创建新连接。打开表单时打开连接,然后使用它在按下按钮时执行所需的命令。
答案 1 :(得分:0)
MySQL Connector / NET有一个错误,它打开第一个连接(或者所有连接,如果禁用了池)慢:bug 80030。
看起来这个bug还没有修复(在8.0.10-rc中);作为一种解决方法,您可以切换到MySqlConnector,这是一种OSS,高性能替代方案。