简体   繁体   English

C#启用基于mysql值的按钮

[英]c# enable button based on mysql value

I would like to know how to enable button based on mysql value. 我想知道如何基于mysql值启用按钮。

I have database called database. 我有一个称为数据库的数据库。 Inside database table users and row uploader (varchar(45)) text in row is "True". 在数据库表用户和行上载器(varchar(45))内部,行中的文本为“ True”。

Here is my code ... but it doesn't work. 这是我的代码...但是不起作用。 Any solution would be great. 任何解决方案都很好。

try
{
    string myConnection = "datasource=localhost;port=3306;username=root;password=pass";
    MySqlConnection myConn = new MySqlConnection(myConnection);

    MySqlCommand SelectCommand = new MySqlCommand("select uploader from database.users where username='" + c.username_txt.Text + "' ;", myConn);
    MySqlDataReader myReader;
    myConn.Open();
    myReader = SelectCommand.ExecuteReader();
    while (myReader.Read())
    {
        Console.WriteLine(myReader.GetString(myReader.GetOrdinal("uploader")));
    }
    string uploader = Console.ReadLine();
    if (uploader == "True")
    {
        uploadToolStripMenuItem.Enabled = true;
    }
    else
        myConn.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
if (uploader == "True")
{
    uploadToolStripMenuItem.Enabled = true;
}
else
{
    myConn.Close();
    uploadToolStripMenuItem.Enabled = false
}

if uploadToolStripMenuItem is already enabled and you want it disabled when uploader != "True" , then you have to disable it explicitly. 如果上uploader != "True"时已经启用了uploadToolStripMenuItem并且您希望将其禁用,则必须显式禁用它。 Something else about your code, uploader won't be assigned any data from the table, but user input from the console (CLI), which doesn't make much sense since your working with buttons (GUI). 关于您的代码的其他信息,将不会为表中的上uploader分配任何数据,而是会从控制台(CLI)分配用户输入,这自从使用按钮(GUI)以来没有多大意义。 to assign the value of the database call to the var, and keep the code like it is, do: 将数据库调用的值分配给var,并保持代码不变,请执行以下操作:

string uploader;
while (myReader.Read())
{
    uploader = myReader.GetString(myReader.GetOrdinal("uploader"))
    Console.WriteLine(uploader);
}

this will set uploader to the value of the uploader column in the last row 这会将上载器设置为最后一行中上载器列的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM