2013-10-22 43 views
0

我想知道如何启用基于mysql值的按钮。c#启用基于mysql值的按钮

我有数据库调用数据库。内部数据库表用户和行上传器(varchar(45))行中的文本是“True”。

这是我的代码...但它不起作用。任何解决方案都会很棒。

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); 
} 
+1

你这是什么意味着_it不工作?任何异常或错误信息? –

+0

你应该把你的连接放在'using'语句中。这将处理关闭连接,即使有异常。 –

+0

请使用[参数化查询](http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html)。也可以使用'using'语句进行​​连接。 –

回答

1
if (uploader == "True") 
{ 
    uploadToolStripMenuItem.Enabled = true; 
} 
else 
{ 
    myConn.Close(); 
    uploadToolStripMenuItem.Enabled = false 
} 

如果uploadToolStripMenuItem已经启用,并且希望它禁用时uploader != "True",那么你必须明确地禁用它。关于您的代码的其他内容,uploader将不会被分配表中的任何数据,而是来自控制台(CLI)的用户输入,从您使用按钮(GUI)开始并没有什么意义。分配数据库调用var值,并保持这样的代码是,这样做:

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

这将在最后一排设置上传给上载列的值

+0

谢谢!这工作非常好! –

+0

哪个部分,第一个代码块还是第二个? –

+0

我是这样做的:http://shrani.si/f/2Z/NP/4sS5eILO/solution.png –

相关问题