经过大量搜索试图找到在我的 WPF 应用程序中使用嵌入式 DB 的最佳解决方案后,我终于确定/发现了使用 MS Access DB 的便利性。我曾经玩过 SQL,但基本上一直遇到 Access 最终解决的错误和问题。因此,为了在导入后使用数据库,我只需将 DataSet 拖到 WPF 窗口上,VS 就会生成一堆允许访问的代码。它工作得很好。
但是只有一个问题,那就是 ZipCode 表查找会导致程序停顿 4-5 秒,包括在按键之间挂起。这消除了一些易用性,我想找到一种方法来加快速度。
我曾想过使用 BackgroundWorker,但我似乎无法找到一种仅在适当的时间将命令传递给它的方法。我什至不确定这是否是最好的解决方案,或者是否有其他方法可以提高速度。
在 DB 文件中,邮政编码作为主键,我将两列(邮政编码和位置)都编入索引,这似乎并没有提高性能。下面的两个函数是通过各种文本框的 OnTextChanged 事件来访问的。
任何建议将不胜感激。
public void LocateZipCode(TextBox source, TextBox destination)
{
LocationsDataSet locationsDataSet = ((LocationsDataSet)this.FindResource("locationsDataSet"));
// Load data into the table ZipCodes. You can modify this code as needed.
LocationsDataSetTableAdapters.ZipCodesTableAdapter locationsDataSetZipCodesTableAdapter = new LocationsDataSetTableAdapters.ZipCodesTableAdapter();
locationsDataSetZipCodesTableAdapter.Fill(locationsDataSet.ZipCodes);
CollectionViewSource zipCodesViewSource = ((CollectionViewSource)(this.FindResource("zipCodesViewSource")));
zipCodesViewSource.View.MoveCurrentToFirst();
try
{
if (source.Text.Length == 5)
{
destination.Text = locationsDataSet.ZipCodes.FindByZipCode(source.Text).Location.ToString();
}
}
catch (NullReferenceException)
{
}
}
#region Area Code Lookup
public void LocateAreaCode(TextBox source, TextBox destination, TextBox destination2 = null)
{
LocationsDataSet locationsDataSet = ((LocationsDataSet)(this.FindResource("locationsDataSet")));
// Load data into the table AreaCodes. You can modify this code as needed.
LocationsDataSetTableAdapters.AreaCodesTableAdapter locationsDataSetAreaCodesTableAdapter = new LocationsDataSetTableAdapters.AreaCodesTableAdapter();
locationsDataSetAreaCodesTableAdapter.Fill(locationsDataSet.AreaCodes);
CollectionViewSource areaCodesViewSource = ((CollectionViewSource)(this.FindResource("areaCodesViewSource")));
areaCodesViewSource.View.MoveCurrentToFirst();
try
{
if (source.Text.Length >= 3 && destination2 != null) //Info tab area code check
{
destination.Text = locationsDataSet.AreaCodes.FindByArea_Code(source.Text).Location.ToString();
destination2.Text = locationsDataSet.AreaCodes.FindByArea_Code(source.Text).Time_Zone.ToString();
}
else if (source.Text.Length >= 3 && destination.Text.Length == 0 && destination2 == null) //Other area code checks
{
destination.Text = locationsDataSet.AreaCodes.FindByArea_Code(source.Text).Location.ToString();
}
else if (source.Text.Length < 3 && destination2 != null) //Info tab area code check
{
destination.Text = "";
destination2.Text = "";
}
else if (source.Text.Length < 3 && destination.Text.Length == 0 && destination2 == null) //Other area code checks
{
destination.Text = "";
if (destination2 != null)
{
destination2.Text = "";
}
}
}
catch (NullReferenceException)
{
destination.Text = "Invalid Area Code";
if (destination2 != null)
{
destination2.Text = "";
}
}
}
#endregion