C# 使用textBox\u TextChanged事件从数据库中提取数据并将其显示到自动完成文本框中的操作变慢
我正在开发一个winform应用程序。我有一个文本框。在这个文本框中,用户可以在其中写入内容。我的工作是匹配每个字母,将文本与数据库进行比较,并显示与数据库中文本相似的10个单词的建议。我所做的是:C# 使用textBox\u TextChanged事件从数据库中提取数据并将其显示到自动完成文本框中的操作变慢,c#,database,autocomplete,textbox,sql-server-ce,C#,Database,Autocomplete,Textbox,Sql Server Ce,我正在开发一个winform应用程序。我有一个文本框。在这个文本框中,用户可以在其中写入内容。我的工作是匹配每个字母,将文本与数据库进行比较,并显示与数据库中文本相似的10个单词的建议。我所做的是: private void textBox1_TextChanged(object sender, EventArgs e) { string a = textBox1.Text; // Autocomplete for textbox
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();
}
但是速度太慢了。有时会崩溃。我需要一个解决方案。我可以做些什么来加快速度???在窗体级别而不是在事件处理程序上打开连接 为了获得最佳性能,请绕过查询处理器
private SqlCeConnection conn;
Form__Load()
{
conn = new SqlCeConnection(connectionString);
conn.Open();
}
我能做些什么来提高速度和效率
你目前的设计很难做到。但是,我认为使用计时器(System.Windows.Forms.Timer)是很常见的
这样,您就不会在每次更改文本时进行查询,但是,在执行之前,请先花一段时间。你能告诉我自动完成控件的设计来源吗对不起,我不明白你的问题。你能告诉我你想知道什么吗?@AnujAutoComplete使用了什么控件?autocomplete extender?可能有,但我不喜欢使用非实体对象。我想将整个上下文加载到单个
数据表
/表视图
中会非常慢。设想一个完整的google数据库保存在一个服务器内存中/TableView
使用SqlCeDataReader.Seek-我如何在表单级别打开连接?你能澄清一下吗???@ErikEJ
private void textBox1_TextChanged(object sender, EventArgs e)
{
timerText.Stop();
timerText.Start();
}
private void TimerText_Ticket(object sender, EventArgs e) // forget what event args type is it
{
//do the operation
timerText.Stop();
}