如何从数据库读取特定文本框中着色文本框背景颜色

时间:2017-03-06 08:50:06

标签: c# mysql database background

我正在尝试通过将指定的颜色读取到特定文本来为文本框着色。

这是我的代码:

using (MySQLHelper LeftCurrent = new MySQLHelper())
{
    string mysql = "Select BandColor as LeftSideCurrentBandColor from band WHERE PnBanda LIKE '" + textBoxLeftSideCurrentBandColor.Text + "'";

    MySqlCommand LeftCurrentCommand = LeftCurrent.GetCommand(mysql);
    using (MySqlDataReader reader = LeftCurrentCommand.ExecuteReader())

    while (reader.Read())
    {
        textBoxLeftSideCurrentBandColor.BackColor = System.Drawing.Color.FromName(reader.GetString(0));
    }

但每次我更改指定的文字时,颜色都不会改变。我怎么能这样做?

我有一个文本框,其中包含其中一个值,N \ A,108/5010 / 0300,108 /5015 / 0200,108/5015/0900。

当文本框中填充了一个值时,我想用指定的颜色对其进行着色。

 1  N\A Red
 2  108/5010/0300   yellow
 3  108/5015/0200   DarkBlue
 4  108/5015/0900   white

2 个答案:

答案 0 :(得分:0)

请在不使用循环的情况下尝试此操作,因为您要检索一个值

using (MySQLHelper LeftCurrent = new MySQLHelper())
{
  string mysql = "Select BandColor as LeftSideCurrentBandColor from band WHERE PnBanda LIKE '" + textBoxLeftSideCurrentBandColor.Text + "'";

  MySqlCommand LeftCurrentCommand = LeftCurrent.GetCommand(mysql);
  textBoxLeftSideCurrentBandColor.BackColor = System.Drawing.Color.FromName((LeftCurrentCommand.ExecuteOracleScalar() ?? "RED").ToString());
}

答案 1 :(得分:0)

如果你获得更多的行,如果该值为null或空字符串,你应该得到一个例外。但你的调试很好,告诉你 任何尝试SELECT TOP(1)或LIMIT 1,某事