在一个文本框自动完成设置 [英] set autocomplete in a textbox

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

问题描述

我在窗口的文本框(名为TextBox1中)形成application.I有一个数据库nn.sdf命名,我想用这个作为自动complete.Whenever源的用户提供了一个输入TextBox1中,它会显示从数据库中建议,由用户给出的。所以我把我的code到 textBox1_TextChanged property.my code输入文本匹配的是在这里:

I have a textbox(named textbox1) in windows form application.I have a database named nn.sdf and i want to use this as a source of auto-complete.Whenever a user gives a input into textbox1,it will show a suggestion from the database, matching with the input text given by user .So i put my code into textBox1_TextChanged property.my code is here:

 private void textBox1_TextChanged(object sender, EventArgs e)
    {
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
        SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\Imon-Bayazid\Documents\nn.sdf");
        con.Open();
        SqlCeCommand cmnd = con.CreateCommand();
        cmnd.CommandType = CommandType.Text;
        cmnd.CommandText = "SELECT top(10)  english FROM dic";        
        SqlCeDataReader dReader;
        dReader = cmnd.ExecuteReader();

        if (dReader.Read())
        {
            while (dReader.Read())
                namesCollection.Add(dReader["english"].ToString());
        }
        else
        {
            MessageBox.Show("Data not found");
        }
        dReader.Close();

        textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
        textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
        textBox1.AutoCompleteCustomSource = namesCollection;
    }

但它只能显示前10 data.i知道我的线路问题。

But it shows only the first 10 data.i know i have problem in the line

  cmnd.CommandText = "SELECT top(10)  english FROM dic";// english is my column name and dic is my table name   

我不知道应该是什么cmnd.CommandText.I想要的自我暗示,只要用户输入任何东西到TextBox1的。
我怎样才能做到这一点?

I don't know what should be the cmnd.CommandText.I want the autosuggestion whenever a user input anything into the textbox1. How can i do this???

推荐答案

如你所知,的CommandText 应该(或可以)是一个SQL语句。请尝试以下

As you know, CommandText should (or could) be an SQL statement. Try the following

int fetchAmount = 10;
string userInput = "abc";
cmnd.CommandText = string.Format("SELECT top ({0}) english FROM dic WHERE english like '{1}%'",
    fetchAmount.ToString(), userInput);

LIKE 是比较文本SQL命令。所以你的情况,你希望所有的结果,其中文本与哪些用户已开始键入

LIKE is an SQL command that compares text. So in your case, you want all results where the text starts with what the user has typed in.

现在我的情况下,有人得到之前,我知道这是不是做到这一点的最好办法。进入数值直接插入SQL语句留下您敞开SQL注入。我会强烈建议你学习和实现存储过程做一个数据库的任何互动。

Now before someone gets on my case, I know this is not the best way to do this. Entering in values directly into a SQL statement leaves you wide open to SQL Injection. I would HIGHLY suggest that you learn and implement Stored Procedures to do any interactions with a database.

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

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