【发布时间】:2021-11-14 00:41:40
【问题描述】:
我已将如下数据带入组合框中。在我的“BRANDS”表中,第一列为brand_id,第二列为brand_name。我在组合框中获取名称,但是在将其保存到数据库时需要获取 id。我该怎么做?
void markaekle() {
SqlCommand komut = new SqlCommand("Select * from MARKALAR", bgl.baglanti());
SqlDataReader dr = komut.ExecuteReader();
while (dr.Read())
{
comboBoxMarka.Properties.Items.Add(dr[1]);
}
bgl.baglanti().Close();
}
我需要使用如下按钮将 id 值保存到数据库中:
private void BtnAnaGrupKaydet_Click(object sender, EventArgs e){
SqlCommand komut = new SqlCommand("INSERT INTO ANA_GRUP (marka_id,anagrup_isim,create_date) values (@p1,@p2,@p3)", bgl.baglanti());
komut.Parameters.AddWithValue("@p1", int.Parse(comboBoxMarka.Text));
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);
}
【问题讨论】:
-
WinForms WPF????
-
是的,WinForms。对不起,我忘了添加
标签: c# winforms combobox selectedvalue