在C#中自动完成的同名 [英] Same Name In autocomplete in c#

查看:81
本文介绍了在C#中自动完成的同名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中创建自动完成功能,但想检索相同的名称,但只显示一个名称
例如

i want to create autocomplete in c# but want to retrive same name but it only display one name
for eg.

id=1
name=james
id=2
name=james



当我想取回詹姆斯时
只给一个詹姆斯
我想取回2张詹姆斯
我该怎么做.
代码是.



when i want to retrive james
it only give one james
i want to retrive 2 james
how i can do it.
code is.

textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = namesCollection;
SqlDataReader dReader;
SqlConnection conn = new SqlConnection("Data Source = .\\SQLEXPRESS; Initial catalog = smart_phone_book; Integrated Security = true");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select distinct Name from smart_book" + " order by Name ";
conn.Open();
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
    while (dReader.Read())
        namesCollection.Add(dReader["Name"].ToString());

}

推荐答案

您正在使用关键字DISTINCT,这意味着它将删除重复项.只需在SQL命令中删除DISTINCT.
You are using the keyword DISTINCT which means it will remove duplicates. Just get rid of DISTINCT in your SQL command.


您已查询了唯一名称,并因此获得了结果.

用途:
You have given a query for unique names and hence the result.

Use:
cmd.CommandText = "Select Name from smart_book" + " order by Name ";


进行排序关键字,它将正常工作.
Just remove the distinct keyword and it will work fine.


这篇关于在C#中自动完成的同名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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