c# - 按钮不发送mysql查询?
问题描述
我可以毫无问题地发送“显示从属状态”查询,但是将相同的逻辑应用于按钮时没有任何反应。为什么?
将 con 重命名为 con2
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
DataSet ds = new DataSet();
private DataTable dt;
string credentials = @"server=172.22.6.10;user id=system;persistsecurityinfo=True;port=3343;database=mysql;Password=975315";
string sqlQuery = @"show slave status";
string sqlstopstart = @"stop slave;";
public void BindData()
{
//Opens the connection and sends "show slave status" command.
MySqlConnection con = new MySqlConnection(credentials);
con.Open();
MySqlCommand cmd = new MySqlCommand(sqlQuery, con);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
adp.Fill(ds); //Fill Dataset.
dt = ds.Tables[0]; //Then assign table to dt.
foreach (DataRow dataRow in dt.Rows) //Checks the server if it is running or not.
{
string value = dataRow.Field<string>("Slave_IO_Running");
if (value == "Yes")
{
Label1.Text = "Working";
}
else
{
Label1.Text = "Not Working";
}
}
//Shows a view of the whole query
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
protected void Button1_Click1(object sender, EventArgs e)
{
MySqlConnection con2 = new MySqlConnection(credentials);
con2.Open();
MySqlCommand cmd = new MySqlCommand(sqlstopstart, con2);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
}
}
解决方案
可能是 Button1_Click1 事件方法缺少适配器填充并实际执行 sql 命令。您可以尝试像在 BindData() 中那样做类似的事情(如果需要,可能实际上重用该方法)
推荐阅读
- python - 带有 2D 切片的 numpy 视图
- java - 为自己的类定义一个单独的接口,然后该类实现它的主要优点是什么?
- react-native - 如何“正确”更新我在 PlayStore 上的应用
- python - 这是什么意思?关键错误:'GC'
- flutter - 400 错误:BadRequest - 从 Flutter 桌面发送到 Firebase 本地存储模拟器的 HTTP 请求
- r - 为什么我的直方图在 x 轴上没有负值?
- android - 在 React Native 中,如何“动态”更改 Android 应用程序图标?
- c# - 捕获 TableLayoutPanel 特定区域的图像
- django - 将数据字典与模型实例解析为 Django ModelForm
- python - 使用 pandas 获取两个数据框之间的匹配值