如何使用数据库填充列表控件并处理选定事件 [英] How to populate list controls with database and working on the selected events

查看:65
本文介绍了如何使用数据库填充列表控件并处理选定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到名为NAME的MSQL数据库库仑的组合框,该组合框用于显示数据库中的所有名称.如果用户选择一个名称",则将在列表框中显示某个项目,但该项目将从数据库中的某个库仑中显示,并且仅显示与该相同名称相关联的库伦.

I have a combobox that is bound to MSQL database Coulomb called NAME it is meant to display all the Names in the Database. If the user selects One Name, a certain item is going to be displayed in the list box, but from a certain coulomb in the database and only those that associate with that same NAME.

推荐答案

您好

1.在组合框的SelectedValueChanged事件中,您可以获取所选的名称.例如:
Hello

1. In SelectedValueChanged event of your ComboBox you can get the selected name. for example:
string name = this.NameCombo.SelectedItem.ToString();


2.取决于 ORM .
如果您使用 EF ,那么它很简单:


2. Depends of the ORM.
If you use EF then it''s simple:

var context = new YourEntities();
var persons = context.Where(p=>p.Name == name);

if(persons.Count()>0)
   foreach(Person p in persons)   
       MyListBox.Items.Add(p.Name);



如果您使用 SP :



If you use SP:

Create Procedure GetNames(
   @Name as nvarchar(50)
)
As
Begin

   Select   *   From   Person
        Where Name = @Name
End



然后,您可以在应用程序中调用它.查看以下内容:存储过程

或者,您可以使用此关键字"C#中的 ADO.Net"



Then you can call it form your application. Look at this: Stored Procedures

Or you can search Google with this keyword "ADO.Net in C#"


这篇关于如何使用数据库填充列表控件并处理选定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆