【问题标题】:How to get id of selected item in combobox C#如何在组合框中获取所选项目的ID C#
【发布时间】:2020-09-30 15:44:22
【问题描述】:

我正在我的 WinForms 项目中使用 ComboBox。
我从 ComboBox 中显示的 SQL 服务器表值中获得属性 LIB
我需要获取 selectedItem 的另一个属性 OID 并将其从第一个表单 (FormMaster) 传递到另一个表单 (FormImport)。

con.Open();

SqlCommand cmd = new SqlCommand("SELECT [ID] FROM [AffecAnalytique].[dbo].[DESCRIPTIF] WHERE [LIBELLE]='" + comboBox3.Text + "' ", con);

SqlDataReader rdr = cmd.ExecuteReader();
if (!rdr.HasRows)
{
    try
    {
        string strOIDe = rdr["OID"].ToString();
        textBox1.Text = rdr["OID"].ToString();
        MessageBox.Show(strOIDe);
    }
    catch (Exception ex) { }

    MessageBox.Show("tesssssssssst");
}
else MessageBox.Show("11111");

con.Close();

【问题讨论】:

  • 警告: SQL 很容易受到注入攻击。你必须解决这个问题。
  • 重新开始。编写直接引用特定硬编码数据库的tsql通常是一个巨大的问题。这种做法将防止在同一实例中使用多个环境,并防止使用不同名称的数据库(例如一个用于 clientx,一个用于 clienty)。您的连接应该确定该数据库存在的位置。

标签: c# sql-server forms winforms


【解决方案1】:

您不会使用这样的 SQL,而是使用参数,并且参数值将被转换为实际类型的 comboBox3.SelectedItem。类型可能是字符串,也可能是对象,显示的文本是该对象的属性。

var selectedValue = comboBox3.SelectedItem as string; // whatever the item type is
if (selectedValue != null)
{
   cmd.Parameters.Add("@libelle", SqlDbType.VarChar).Value = selectedValue;
   // your code ... 
}

这是一个使用 SQL Server 北风示例数据库的简化完整示例:

void Main()
{
    DataContext db = new DataContext(@"server=.\SQLexpress2012;trusted_connection=yes;database=Northwind");
    
    Table<Category> Categories = db.GetTable<Category>();
    Table<Product> Products = db.GetTable<Product>();
   
    Form f = new Form { Text="ComboBox ornek", Height=200, Width=500 };
    ComboBox cb1 = new ComboBox{ Left=10, Top=10, Width=450, Font=new Font("Courier New",8) };
    ComboBox cb2 = new ComboBox{ Left=10, Top=60, Width=450, Font=new Font("Courier New",8) };
    
    f.Controls.AddRange( new Control[] {cb1, cb2} );

    cb1.DataSource = Categories.ToList();
    cb1.ValueMember = "CategoryId";
    cb1.DisplayMember = "CategoryName";
    cb1.SelectedIndex = -1;

    cb1.SelectedIndexChanged += (sender, args) => { 
    
    var selectedCategory = ((ComboBox)sender).SelectedItem as Category;
    cb2.DataSource = null;
    cb2.Items.Clear();
    if (selectedCategory != null)
    {
      cb2.DataSource = Products.Where (p => p.CategoryId == selectedCategory.CategoryId).ToList();
      cb2.DisplayMember = "ProductName";
      cb2.ValueMember = "ProductId";
    }
    };
    
    f.Show();
}


[Table(Name = "Categories")]
public class Category
{
    [Column]
    public int CategoryId { get; set; }
    [Column]
    public string CategoryName { get; set; }
    [Column]
    public string Description { get; set; }
}

[Table(Name = "Products")]
public class Product
{
    [Column]
    public int ProductId { get; set; }
    [Column]
    public string ProductName { get; set; }
    [Column]
    public int CategoryId { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2019-10-27
    • 1970-01-01
    • 2017-04-28
    • 2015-08-13
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多