c# 如何连接和断开 mysql 数据库
c# how to connect and disconnect mysql database
this is the image of my form
我想要的是当我点击连接按钮时,到 MySQL 的连接被打开并且我可以使用连接的数据库并且所有文本框都被禁用,如果我点击断开连接,我想关闭连接并且用户将无法使用连接的数据库,并且所有文本框都已启用。
谁能帮我做这个验证!!
这是我在连接按钮中的代码:
连接是MySQL连接全局变量
connection = new MySqlConnection(strConnection);
if(connection.State == ConnectionState.Closed)
{
connection.Open();
}
这是我在断开连接按钮中的代码:
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
This does not work because when the disconnect button is selected the connection variable becomes null, which I do not want.我想维护连接变量的状态。
您可以将连接变量存储在会话状态中,我认为在您调用关闭连接按钮之前页面已 post 返回。所以变量变为空
前任:
打开连接:
connection = new MySqlConnection(strConnection);
if(connection.State == ConnectionState.Closed)
{
connection.Open();
}
Session["connection"] = connection
关闭连接时:
connection = (MySqlConnection)Session["connection"];
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
或者您可以将连接变量修饰符更改为静态
this is the image of my form
我想要的是当我点击连接按钮时,到 MySQL 的连接被打开并且我可以使用连接的数据库并且所有文本框都被禁用,如果我点击断开连接,我想关闭连接并且用户将无法使用连接的数据库,并且所有文本框都已启用。
谁能帮我做这个验证!!
这是我在连接按钮中的代码:
连接是MySQL连接全局变量
connection = new MySqlConnection(strConnection);
if(connection.State == ConnectionState.Closed)
{
connection.Open();
}
这是我在断开连接按钮中的代码:
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
This does not work because when the disconnect button is selected the connection variable becomes null, which I do not want.我想维护连接变量的状态。
您可以将连接变量存储在会话状态中,我认为在您调用关闭连接按钮之前页面已 post 返回。所以变量变为空 前任: 打开连接:
connection = new MySqlConnection(strConnection);
if(connection.State == ConnectionState.Closed)
{
connection.Open();
}
Session["connection"] = connection
关闭连接时:
connection = (MySqlConnection)Session["connection"];
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
或者您可以将连接变量修饰符更改为静态