C# 如何从读取特定文本框的数据库中为文本框背景色上色
我正试图通过读取指定给特定文本的颜色来给文本框上色 这是我的代码:C# 如何从读取特定文本框的数据库中为文本框背景色上色,c#,mysql,database,background,C#,Mysql,Database,Background,我正试图通过读取指定给特定文本的颜色来给文本框上色 这是我的代码: using (MySQLHelper LeftCurrent = new MySQLHelper()) { string mysql = "Select BandColor as LeftSideCurrentBandColor from band WHERE PnBanda LIKE '" + textBoxLeftSideCurrentBandColor.Text + "'"; MySqlCommand L
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());
}
请在不使用循环的情况下尝试此操作,因为您将正确检索一个值
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());
}
如果您得到更多的行,如果该值为null或空字符串,您应该得到一个异常。但是你的调试是好的,就像我告诉你的那样
任何尝试选择TOP(1)或LIMIT 1,something如果您得到更多行,如果该值为null或空字符串,您应该会得到一个异常。但是你的调试是好的,就像我告诉你的那样
尝试选择TOP(1)或LIMIT 1,您是否调试了代码?对于您阅读的每一行。。您将重置同一框的颜色…是的,但我没有得到任何结果。请调试并查看reader.GetString(0)的值@imsome1如果我调试,我会得到颜色深蓝色,如果textbox.text是108/5015/0200,但是如果我选择的textbox的值不同于108/5015/0200,我会得到什么?你调试了你的代码吗?对于你读到的每一行。。您将重置同一框的颜色…是的,但我没有得到任何结果。请调试并查看reader.GetString(0)的值@imsome1如果我调试,如果textbox.text是108/5015/0200,我会得到颜色深蓝色,但是如果我选择的textbox的值不同于108/5015/0200,我不会得到任何方法?'MySqlCommand'不包含'ExecuteOracleScalar'的定义,并且没有扩展方法'ExecuteOracleScalar'接受类型为'MySqlCommand'的第一个参数可以找到(您是否缺少using指令或程序集引用?)我在那里做了一些更改,对于除N\a之外的所有颜色的作品。我从banda中得到“选择CuloareBanda作为LeftSideCurrentBandColor”,其中PnBanda类似于'N\\A'“如何做?'MySqlCommand'不包含'ExecuteOracleScalar'的定义,并且找不到接受'MySqlCommand'类型的第一个参数的扩展方法'ExecuteOracleScalar'(您是否缺少using指令或程序集引用?)我在那里做了一些更改,对于除N\a之外的所有颜色,大多数都做了更改。我得到了这样一个“从banda中选择CuloareBanda作为LeftSideCurrentBandColor,其中PnBanda类似于'N\\a'”