简体   繁体   English

如何通过使用SQL查询将所有列值(已应用不同的值)检索到列表框中?

[英]How to retrieve all the column values(distinct had been applied) into listbox by using sql query?

This might be simple one and also this has been answered before but I don't know how to look for the exact method. 这可能很简单,而且之前已经回答过,但是我不知道如何寻找确切的方法。

-> Am having two list box in my form. ->我的表单中有两个列表框。 One is for product name and other one is for Company name. 一种是产品名称,另一种是公司名称。

-> On page load, Listbox1 will retrieve values from database(Product name) . ->在页面加载时, Listbox1将从database(Product name)检索值。 Once item has been selected from listbox1 , the respective company names should be fetched in listbox2 . listbox1选择项目后,应在listbox2获取相应的公司名称。

For eg : Database name is Motor 例如 :数据库名称是Motor

Here is my table named as "Register" and it contains two columns they are, 这是我的表格,名称为“ Register”,其中包含两列,

Productname        Companyname

Car                 Bmw
Bike                Bmw
Car                 Honda
Bike                Honda

My Question is 我的问题是

I retrieved product name details into listbox1 , here the thing is i don't want to repeat the same items, so I used Distinct like this, 我将产品名称详细信息检索到listbox1 ,这是我不想重复相同的项目,所以我像这样使用Distinct,

Select Distinct Productname from Register

Now if i select car from listbox 1 then the respective company names should be display in listbox2. 现在,如果我从列表框1中选择汽车,则相应的公司名称应显示在列表框2中。 But what am getting is only Honda on my listbox2 and am not getting BMW. 但是得到的只是本田车上的本田而没有得到宝马。

Sqlcommand cmd=new Sqlcommand("Select Companyname from Register where Productname='"+listbox1.selecteditem+"'",con);
Sqldatareader dr=cmd.Executereader();
while(dr.Read())
{
   string a=dr.GetString(0);
   listbox2.items.add(a);
}

This is the query which is used to retrieve values into listbox2. 这是用于将值检索到listbox2中的查询。 I used datareader to read and get the values from the database. 我使用datareader读取并从数据库中获取值。

I don't know exactly how to do this. 我不知道该怎么做。 Hope am not confusing you.So any help would be more helpful to me and thanks in advance. 希望不要让您感到困惑,因此任何帮助都会对我有所帮助,并在此先感谢您。

Have you tried with, 你试过了吗

    Select Companyname from Register 
    where Productname='"+listbox1.GetItemText(listbox1.SelectedItem)+"'

Try this.. 尝试这个..

Sqlcommand cmd=new Sqlcommand("Select Companyname from Register where Productname='"+listbox1.selecteditem.toString()+"'",con);
Sqldatareader dr=cmd.Executereader();

listbox2.items.clear();

while(dr.Read())
{
   string a=dr['Companyname'].toString();
   listbox2.items.add(a);
}

ur using string a=dr.GetString(0); ur使用字符串a = dr.GetString(0); listbox2.items.add(a); listbox2.items.add(a);

which will fetch only first row from table.. 这将只从表中获取第一行。

use string a=dr["companyname"].toString(); 使用字符串a = dr [“ companyname”]。toString(); listbox2.items.add(a); listbox2.items.add(a);

this should solve your problem. 这应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用SQL查询从另一个列表框向列表框添加值? - How to add values to listbox from another listbox using SQL query? 使用LINQ选择列中的所有不同值 - Select All distinct values in a column using LINQ FluentNhibernate:查询以检索不同的值 - FluentNhibernate: Query to retrieve distinct values 如何从SQL数据库的列中检索子​​字符串到ListBox中 - How to retrieve a sub string from from a column in SQL database into a ListBox 如何将SQL中的逗号分隔值分别检索到ListBox - How to retrieve commas delimited values in SQL to a ListBox separately 如何使用LINQ执行查询,该查询必须使一列与众不同但包括另一列的所有信息 - How to perform a query using LINQ that must make one column distinct but includes all information of another column 选择查询以使用linq根据不同的值获取所有值 - select query to get all values based on distinct values using linq 如何在下拉列表中获取datagrid列中sql表列的不同值? - How to get distinct values of sql table column in datagrid column as dropdown? 如何汇总列表框列中的所有值并将其显示在文本块中? - How to sum up all values in a listbox column and display it in a textblock? 如何检索列表框中选定项目的选定值? - How to retrieve selected values for selected items in a ListBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM