You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#开发如何获取ComboBox选中项对应的关联数据库ID值

问题核心原因

你当前的代码仅将品牌名称存入了ComboBox选项集合,没有关联存储对应的brand_id,所以直接把ComboBox的Text属性转int存入数据库的逻辑必然会报错。

推荐解决方案:使用数据绑定关联ID和显示名称

该方案无需自定义类,直接利用控件自带的绑定属性实现显示值和实际值的分离。

1. 修改ComboBox数据加载代码

void markaekle() {
    // 按需查询需要的字段,不要用select *
    SqlCommand komut = new SqlCommand("Select brand_id, brand_name from MARKALAR", bgl.baglanti());
    SqlDataAdapter da = new SqlDataAdapter(komut);
    DataTable dt = new DataTable();
    da.Fill(dt);
    // 绑定数据源
    comboBoxMarka.Properties.DataSource = dt;
    // 指定显示的字段为品牌名称
    comboBoxMarka.Properties.DisplayMember = "brand_name";
    // 指定实际取值的字段为品牌ID
    comboBoxMarka.Properties.ValueMember = "brand_id";
    bgl.baglanti().Close();
}

2. 修改按钮点击事件的ID取值逻辑

新增空值判断避免用户未选择品牌时报错,直接取SelectedValue属性即可拿到对应的品牌ID:

private void BtnAnaGrupKaydet_Click(object sender, EventArgs e){
    // 校验是否选中品牌
    if(comboBoxMarka.SelectedValue == null) {
        MessageBox.Show("请先选择对应品牌", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
    SqlCommand komut = new SqlCommand("INSERT INTO ANA_GRUP (marka_id,anagrup_isim,create_date) values (@p1,@p2,@p3)", bgl.baglanti());
    // 直接读取选中项绑定的ID值
    komut.Parameters.AddWithValue("@p1", (int)comboBoxMarka.SelectedValue);
    komut.Parameters.AddWithValue("@p2", txtAnaGrup.Text);
    komut.Parameters.AddWithValue("@p3", dateAnaGrup.DateTime);
    komut.ExecuteNonQuery();
    bgl.baglanti().Close();
    MessageBox.Show("Ana Grup Sisteme Eklendi", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
备选方案:自定义实体类存储

如果你不想用数据绑定,也可以自定义品牌实体类关联ID和名称:

  1. 先定义Brand类:
public class Brand
{
    public int Id { get; set; }
    public string Name { get; set; }
    // 重写ToString方法,让ComboBox显示品牌名称
    public override string ToString()
    {
        return Name;
    }
}
  1. 修改加载逻辑:
void markaekle() {
    SqlCommand komut = new SqlCommand("Select brand_id, brand_name from MARKALAR", bgl.baglanti());
    SqlDataReader dr = komut.ExecuteReader();
    while (dr.Read())
    {
        comboBoxMarka.Properties.Items.Add(new Brand {
            Id = Convert.ToInt32(dr["brand_id"]),
            Name = dr["brand_name"].ToString()
        });
    }
    bgl.baglanti().Close();
}
  1. 修改取值逻辑:
private void BtnAnaGrupKaydet_Click(object sender, EventArgs e){
    if(comboBoxMarka.SelectedItem is not Brand selectedBrand) {
        MessageBox.Show("请先选择对应品牌", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
    SqlCommand komut = new SqlCommand("INSERT INTO ANA_GRUP (marka_id,anagrup_isim,create_date) values (@p1,@p2,@p3)", bgl.baglanti());
    komut.Parameters.AddWithValue("@p1", selectedBrand.Id);
    komut.Parameters.AddWithValue("@p2", txtAnaGrup.Text);
    komut.Parameters.AddWithValue("@p3", dateAnaGrup.DateTime);
    komut.ExecuteNonQuery();
    bgl.baglanti().Close();
    MessageBox.Show("Ana Grup Sisteme Eklendi", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

内容的提问来源于stack exchange,提问作者Sinem Hür

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 06:24:05