c# - 你好。谁能告诉我为什么我的代码 Section='"+lblSection.Text+"' 参数不起作用
问题描述
当我运行它时,它不会填充我的组合框中的项目。
public void FILLREPRESENTATIVE()
{
try // fill combobox Party with corresponding Representative Section
{
MySqlCommand command = new MySqlCommand("SELECT CONCAT(Firstname,' ',LastName) AS Full_Name, Section,
Position FROM candidatetable WHERE Section='"+lblSection.Text+"' AND Position= 'Representative'", connection);
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
cbRepresentative.Items.Add(reader.GetString("Full_Name"));
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
如果我使用这种代码: Section='ict' 它会填满我的组合框。但我想为我的部分使用标签文本。
解决方案
尝试使用这行代码:
MySqlCommand cmd = new MySqlCommand("SELECT CONCAT(Firstname,' ',LastName) AS Full_Name, Section,
Position FROM candidatetable WHERE Section=@section AND Position= 'Representative'", con);
cmd.Parameters.AddWithValue("@section", lblSection.Text);
推荐阅读
- java - 使用 Maven 单独执行测试
- c# - 使用动态列创建临时表
- java - Windows 缩放和 java swing gui 缩放
- mysql - 修改模型存储实体框架 6 和 MySQL 的架构
- loops - 用于循环奇怪结果的松树脚本
- multithreading - 我们可以并且应该将操作系统线程固定到 CPU 内核吗?
- snowflake-cloud-data-platform - 如何传递和调用 Clob 数据类型的雪花过程
- javascript - ajax调用控制器时如何防止加载器和屏幕闪烁?
- node.js - 在 nodejs 中高效的 redis 最近聊天存储和检索
- xamarin - 获取 apk 文件的信息以检查版本号