【发布时间】:2019-04-21 02:11:03
【问题描述】:
我有一个连接到 SQL Server 的 WPF 应用程序;虽然我将最多 10 条记录加载到我的 DataGrid 中,但我的应用程序工作正常并且响应速度太快,但是当我加载所有行(几乎是 1000 行)时,我的应用程序需要大约 15 秒来加载并冻结整个 UI。
但是当我在 SQL Server 中执行相同的查询时,加载这 1000 行只需要大约 00:00:00.490 秒,这太快了。为了避免 UI 冻结和快速执行查询,我已经做了如下操作。我做错了什么?由于我是 C# 世界的新手,请使用代码 sn-ps 进行指导。
// Calling function to load data into DataGrid in a new thread,
// to make UI responsive.
String qry = "select * from institutes_tbl"
DataGrid dg = MainDataGrid;
Thread thread = new Thread(() => FunDataGrid_DataView(dg, qry));
thread.IsBackground = true;
thread.Start();
但不幸的是,我的 UI 显示消息“无响应”。下面是函数定义:
public void FunDataGrid_DataView(DataGrid dg, string qry)
{
Application.Current.Dispatcher.BeginInvoke
(
DispatcherPriority.Background,
new Action(() =>
{
try
{
con = new SqlConnection(con_string);
cmd = new SqlCommand(qry, con);
cmd.CommandTimeout = 12 * 3600;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dg.ItemsSource = dt.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!",
MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
));
}
这是我的 XAML:
<DataGrid x:Name="DataGrid_View"
MouseLeftButtonUp="DataGrid_View_MouseLeftButtonUp"
ItemsSource="{Binding DATA_TBL}"
LoadingRow="DataGrid_View_LoadingRow" Grid.Row="2"
Grid.Column="0" ScrollViewer.CanContentScroll="False"
AutoGenerateColumns="False" CanUserAddRows="False"
Background="#7F179DB2" CellStyle="{StaticResource CellStyle}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding NAME}" Width="5*"/>
<DataGridTextColumn Header="Father Name"
Binding="{Binding F_NAME}" Width="5*"/>
<DataGridTextColumn Header="CNIC"
Binding="{Binding CNIC}" Width="5*"/>
</DataGrid.Columns>
</DataGrid>
我希望我的 UI 能够快速响应并快速加载数据。我现在正在本地主机上工作。
【问题讨论】:
-
您需要所有列吗?如果不需要,则仅在 select 子句中提及这些列。也分配没有线程的数据网格
-
@S.Akbari 因为我也在使用 Dispatcher 和 New Thread 我做错了什么?你能多指导一点吗?我已经浏览了您提供的链接。
-
@SatishPai 是的!我需要所有列的全部数据。
-
是的,您正在生成一个新线程,然后再次在 UI 线程上执行实际的数据库访问 (Application.Current.Dispatcher)
标签: c# sql-server wpf user-interface