[英]how to reduce the roundtrip to database in the keypress event for textbox in C#
In my Windows Application i am using Text_box with List_box. 在我的Windows应用程序中,我将Text_box与List_box一起使用。 When event fired in keypress event it retrieve the data and listed in listbox 当事件在keypress事件中触发时,它会检索数据并在列表框中列出
My problem is: For every keypress event hitting the database and application became slower. 我的问题是:对于每一次击键事件,数据库和应用程序都会变慢。
How can i reduce the roundtrip the database in keypress event? 如何减少按键事件中的数据库往返次数?
private void txtVehicleNo_TextChanged(object sender, EventArgs e)
{
try
{
lstVehicleNo.Visible = true;
if (!string.IsNullOrEmpty(txtVehicleNo.Text))
{
dsResult = oclsBal.GetVhNo(txtVehicleNo.Text);
if (dsResult.Tables.Count > 0)
{
DataRow dr = dsResult.Tables[0].NewRow();
lstVehicleNo.DataSource = dsResult.Tables[0];
lstVehicleNo.DisplayMember = "VehicleNo";
lstVehicleNo.ValueMember = "VehicleId";
lstVehicleNo.SelectedIndex = 0;
dsResult.Tables.Clear();
}
}
if (lstVehicleNo.Items.Count == 0)
{
lstVehicleNo.Visible = false;
}
if (txtVehicleNo.Text.Length == 0)
{
lstVehicleNo.Visible = false;
}
if (lstVehicleNo.Items.Count == 1)
{
if (txtVehicleNo.Text.ToUpper() == lstVehicleNo.Items[0].ToString().ToUpper())
lstVehicleNo.Visible = false;
}
}
catch (Exception ex)
{
throw ex;
}
} }
Only retrieve the list data after a timeout after the key is pressed, when a key is pressed, reset this timeout. 仅在按键后超时后才检索列表数据,当按键时,请重置该超时。 This will mean only getting the data when there is a reasonable enough pause, when the user has stopped typing. 这意味着仅当用户停止键入时有足够的足够的停顿时才获取数据。
Alternatively use an auto-complete style box that pre-fetches any expected list items. 或者,使用自动完成的样式框来预取所有预期的列表项。
For the first key-press, get all the records starting with that letter and keep it in cache or local memory and for further key presses you this cache. 对于第一次按键,获取以该字母开头的所有记录,并将其保存在缓存或本地内存中,再按一次此缓存。 Since now you have a subset of data in local storage, you should get better performance. 由于现在您在本地存储中有一部分数据,您应该获得更好的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.