【发布时间】:2016-02-17 00:00:38
【问题描述】:
我目前正在处理我的项目,而我一直面临的问题是当您在数据库中搜索数据时,性能会变慢并且没有响应。
我已经创建了一个线程,但它仍然让我头疼
//to start the thread when textbox has change
private void textBox1_TextChanged(object sender, EventArgs e)
{
ThreadStart thread2Start = new ThreadStart(searchMyData);
Thread t2 = new Thread(thread2Start);
t2.Start();
}
public void searchMyData()
{
if (radGridView1.InvokeRequired)
{
radGridView1.Invoke(new Action(() =>
{
MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
try
{
if(!(textBox1.Text=="Search Students"))
{
cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * from studenttable where studname like'" + textBox1.Text + "%' OR studnum like'" + textBox1.Text + "%' OR studcourse like'" + textBox1.Text + "%' OR studemail like'" + textBox1.Text + "%' OR studsec like'" + textBox1.Text + "%' OR studgender like'" + textBox1.Text + "%' ";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
radGridView1.DataSource = ds.Tables[0].DefaultView;
connection.Close();
}
}
}
}
}
那么这里有什么解决方案可以提高我的程序的性能?
【问题讨论】:
-
只要你输入 new Thread(),就结束了;您的项目已有旧代码。 shop.oreilly.com/product/0636920030171.do
标签: c# database multithreading