首页 > 解决方案 > 按钮不发送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);

    }
}

标签: c#asp.net

解决方案


可能是 Button1_Click1 事件方法缺少适配器填充并实际执行 sql 命令。您可以尝试像在 BindData() 中那样做类似的事情(如果需要,可能实际上重用该方法)


推荐阅读