提问人:Milos Dimitrijevic 提问时间:11/14/2023 最后编辑:marc_sMilos Dimitrijevic 更新时间:11/14/2023 访问量:40
使用大量数据填充 DataGridView 时性能 VB.NET < - SQL 性能降低
Slow performance in populating DataGridView with large amount of data VB.NET <- SQL
问:
我在 WPF VB.NET 项目中填充数据网格时遇到问题。 这是我用于在本地 SQL Server 数据库中调用存储过程的代码:
Public Class SQL
Private Const ConnectionString As String = "Data Source=(localdb)\TestVFE;Initial Catalog=testVFE;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
Private Shared Connection As New SqlConnection(ConnectionString)
Public Shared Sub OpenConnection()
If Connection.State = ConnectionState.Closed Then
Connection.Open()
End If
End Sub
Public Shared Sub CloseConnection()
If Connection.State = ConnectionState.Open Then
Connection.Close()
End If
End Sub
Public Shared Function GetLast1000Rows() As DataTable
OpenConnection()
Dim cmd As New SqlCommand("GetLast1000Rows", Connection)
cmd.CommandType = CommandType.StoredProcedure
Dim adapter As New SqlDataAdapter(cmd)
Dim results As New DataTable()
adapter.Fill(results)
CloseConnection()
Return results
End Function
这是主窗口:
Class MainWindow
Private Sub ResultsGrid_Loaded(sender As Object, e As RoutedEventArgs) Handles ResultsGrid.Loaded
Dim dataTable As DataTable = SQL.GetLast1000Rows
ResultsGrid.RowHeaderStyle = DataGridViewRowHeadersWidthSizeMode.EnableResizing
ResultsGrid.ItemsSource = dataTable.DefaultView
End Sub
End Class
在调试模式下,要从 SQL Server 获取数据,大约需要 500-800 毫秒,但是当执行整个代码时,它需要等待大约 2 分钟才能用 123 列填充 2000 行(表中最大的数据是 4 列 nvarchar 40,其余数据要小得多)。
有人可以帮我改进吗?
答: 暂无答案
评论
DataGridViewRowHeadersWidthSizeMode
DataGridView
Option Strict Off
DataGridView
DataGridView
ItemsSource
DataGrid
ResultsGrid.RowHeaderStyle = DataGridViewRowHeadersWidthSizeMode.EnableResizing
没有意义,也不应该编译