c# - 如何从数据库读取特定文本框为文本框背景颜色着色

标签 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

最佳答案

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

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());
}

关于c# - 如何从数据库读取特定文本框为文本框背景颜色着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42621181/

相关文章:

c# - Global 和 IProcessHostPreloadClient 中的 Application_Start

javascript - sequelize - 获取包含模型的 AVG

mysql - SQL 选择一月中的所有日期

c++ - 为 Sql 服务器实现 odbc 包装器。以字符形式读取数据库数据或要求驱动程序将数据转换为 C 类型

C# csc 从内存编译源代码

c# - C# 中的泛型与非泛型性能

php - 重定向循环错误 laravel auth

mysql - 在 mySQL 中选择不同三元组的补集

ruby-on-rails - rails - float 精度

c# - 开发人员视频