在一个文本框中自动完成一个数据库中的多个值 [英] autocomplete for more than one value form the database in a one textbox

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

问题描述

我希望在一个文本框中从数据库自动完成一个以上值.

我从数据库尝试了一个值并正常工作..

但是我想在该文本框中添加更多的值,从db

这是db中一个值自动完成的代码


i want autocomplete for more than one value form the database in one textbox.

i tried it for one value from the db and working fine..

but i want to put more values form db in that textbox

here is the code for one value autocomplete from db


// autocomplete start here
           AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

           OleDbDataReader dReader;
           OleDbConnection conn = new OleDbConnection();
           conn.ConnectionString = connectionString;
           OleDbCommand cmd = new OleDbCommand();
           cmd.Connection = conn;
          cmd.CommandType = CommandType.Text;
          //cmd.CommandText = "Select distinct [Name] from [Names]" + " order by [Name] asc";
          cmd.CommandText = "Select [MedicineName] from [medicines] order by [MedicineName] asc";
           conn.Open();
           dReader = cmd.ExecuteReader();
           if (dReader.HasRows == true)
           {
               while (dReader.Read())
               namesCollection.Add(dReader["MedicineName"].ToString());
           }
           else
           {
               MessageBox.Show("Data not found");
           }
           dReader.Close();

           txtperscription.AutoCompleteMode = AutoCompleteMode.Suggest;

           txtperscription.AutoCompleteSource = AutoCompleteSource.CustomSource;

           //txtName.AutoCompleteCustomSource = namesCollection;
           txtperscription.AutoCompleteCustomSource = namesCollection;

推荐答案

字符串集合未绑定到查询.您可以只运行两个查询并将所有结果添加到其中:

The string collection is not tied to a query. You can just run two queries and add all the results to it:

...
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
 
AddQueryToAutocomplete(namesCollection, "Select [MedicineName] from [medicines] order by [MedicineName] asc");
AddQueryToAutocomplete(namesCollection, "Select distinct [Name] from [Names] order by [Name] asc");
          
txtperscription.AutoCompleteMode = AutoCompleteMode.Suggest;
            
txtperscription.AutoCompleteSource = AutoCompleteSource.CustomSource;
 
txtperscription.AutoCompleteCustomSource = namesCollection;

// ...

void AddQueryToAutocomplete(AutoCompleteStringCollection namesCollection, string query){
            OleDbDataReader dReader; 
            OleDbConnection conn = new OleDbConnection(); 
            conn.ConnectionString = connectionString; 
            OleDbCommand cmd = new OleDbCommand(); 
            cmd.Connection = conn; 
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = query; 
            conn.Open(); 
            dReader = cmd.ExecuteReader();
            if (dReader.HasRows == true) 
            { 
                while (dReader.Read())   
                namesCollection.Add(dReader[0].ToString()); 
            }
            else 
            { 
                MessageBox.Show("Data not found"); 
            } 
            dReader.Close();
}



您还应该考虑缓存那些查询的结果,因为对几乎是静态数据的每个自动完成都进行两次查询查找是非常低效的.



You should also think about caching the results of those queries, as doing two query lookups every autocomplete for what is almost static data is quite inefficient.


这篇关于在一个文本框中自动完成一个数据库中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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