我正在开发一个 winform 应用程序。我有一个文本框。在这个文本框中,用户可以写一些东西。我的工作是匹配每个字母并将文本与数据库进行比较,并显示与数据库文本相似的 10 个单词的建议.我所做的是:
private void textBox1_TextChanged(object sender, EventArgs e)
{
string a = textBox1.Text;
// Autocomplete for textbox
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
SqlCeConnection con = new SqlCeConnection(@"Data Source=" + Directory.GetCurrentDirectory() + @"\Database\ghfghfgh.sdf;Password=1020;");
con.Open();
SqlCeCommand cmnd = con.CreateCommand();
cmnd.CommandType = CommandType.Text;
int fetchAmount = 10;
string userInput = textBox1.Text;
cmnd.CommandText = string.Format("SELECT top ({0}) english FROM dic WHERE english like '{1}%'",
fetchAmount.ToString(), userInput);
SqlCeDataReader dReader=null;
dReader = cmnd.ExecuteReader();
if (dReader !=null)
{
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;
// end of autocomplete
con.Close();
}
但它变得太慢了。有时它会崩溃。我需要一个解决方案。我该怎么做才能让它更快????