在 dgv 加载时显示加载动画

Showing loading animation while dgv loads

更新代码。我认为它很接近,但 dgv 没有加载任何数据。

Public Class 关闭作业 Public sqlcon 作为字符串 = My.Settings.New_Assembly_AccessConnectionString Public con As New SqlConnection(sqlcon) Public 作业作为字符串

Public Async Function GetDataAsync(ByVal sql As String, ByVal sqlcon As String) As Task(Of DataTable)
    Dim dt As New DataTable
    Dim cmd As New SqlCommand(sql, con)
    Using da = New SqlDataAdapter(sql, sqlcon)
        da.SelectCommand = cmd
        cmd.Parameters.AddWithValue("@Job2", job)

        Await Task.Run(Function()
                           da.Fill(dt)
                       End Function)
    End Using

    Return dt
End Function


Public Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
    Refresh.Visibility = Windows.Visibility.Visible
    txtJob.IsEnabled = False
    btnEnter.IsEnabled = False

    Try
        job = txtJob.Text
        Dim sqlcon As String = My.Settings.New_Assembly_AccessConnectionString
        Dim sql As String
       
        sql = "SELECT isnull(p.[CLASS],j.class) as 'CLASS',isnull([SERIAL_NUMBER],left(year(d.cup_mfg_date),2) + d.cup_serial) as SERIAL, " & _
                              "isnull([CUP_DATE],d.cup_mfg_date) as CUPDATE, isnull([CUP_PART_NUM],left([Cup#],charindex('-',Cup#)-1)) as PARTNUM, isnull(p.[LATERAL_VALUE], " & _
                               "d.lateral_value * 10000) as LATVALUE, isnull([LAT_UPPER],0) as LAT_UPPER,isnull([LAT_LOWER],0) as LAT_LOWER, " & _
                               "isnull(p.[BEFORE_WEIGHT],0) as BEFORE_WEIGHT, isnull(p.[AFTER_WEIGHT],0) as AFTER_WEIGHT,isnull([GREASE_UPPER],0) as GREASE_UPPER, " & _
                               "isnull([GREASE_LOWER],0) as GREASE_LOWER,isnull(p.[SPACER_MEASURE], d.Spacer_Measure) as SPACER,isnull([QTY_SPACER_CHANGE],0) as 'CHANGES', " & _
                               "isnull([LATERAL_DATE_TIME],'1999-11-11 11:11:11.111') as LATERAL_DATE_TIME, " & _
                               "isnull([GREASE_DATE_TIME],'1999-11-11 11:11:11.111') as GREASE_DATE_TIME, isnull([LINE_NUM],d.linenum) as LINE, " & _
                               "isnull(BAD_PART, 'BAD_INFO') as Result, isnull([AIR_PRESSURE1], 0) as PRESSURE " & _
                               "FROM [NB_ASSEMBLY].[dbo].[PieceData] p full outer JOIN New_Assembly_Access.dbo.Tbl_Data AS d " & _
                               "ON substring(d.zbarcode,3,6) = right(p.serial_number,6) and month(d.cup_mfg_date) = month(p.cup_date) and " & _
                               "right(d.zbarcode,10) = right(p.cup_part_num,10) join new_assembly_Access.dbo.tbl_job j on j.job# = d.job# where d.job# = @Job2"

        con.Open()
        Dim data = Await GetDataAsync(sql, sqlcon)

        dgvJob.DataContext = data
        dgvJob.AutoGenerateColumns = True
        dgvJob.CanUserAddRows = False

    Catch ex As Exception

    End Try
    Refresh.Visibility = Windows.Visibility.Hidden

End Sub

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    Refresh.Visibility = Windows.Visibility.Hidden
    txtJob.Focus()

End Sub

结束Class

这是您即时修改的代码,从这里开始,因为我不知道是否运行良好,或者是否需要在没有您的数据库和其他参数的情况下进行更改。

Public con As New SqlConnection
Public job As String

Private Sub ReadDatabase()

    Dim bgThread As Threading.Thread = New Threading.Thread(Sub()

                                                                Dim sqlcon As String = My.Settings.New_Assembly_AccessConnectionString
                                                                Dim sql As New SqlCommand
                                                                Dim da As New SqlDataAdapter
                                                                Dim ds As New DataSet

                                                                Using con = New SqlConnection(sqlcon)
                                                                    con.Open()

                                                                    sql = New SqlCommand("SELECT isnull(p.[CLASS],j.class) as 'CLASS',isnull([SERIAL_NUMBER],left(year(d.cup_mfg_date),2) + d.cup_serial) as SERIAL, " &
                                                                                              "isnull([CUP_DATE],d.cup_mfg_date) as CUPDATE, isnull([CUP_PART_NUM],left([Cup#],charindex('-',Cup#)-1)) as PARTNUM, isnull(p.[LATERAL_VALUE], " &
                                                                                              "d.lateral_value * 10000) as LATVALUE, isnull([LAT_UPPER],0) as LAT_UPPER,isnull([LAT_LOWER],0) as LAT_LOWER, " &
                                                                                              "isnull(p.[BEFORE_WEIGHT],0) as BEFORE_WEIGHT, isnull(p.[AFTER_WEIGHT],0) as AFTER_WEIGHT,isnull([GREASE_UPPER],0) as GREASE_UPPER, " &
                                                                                              "isnull([GREASE_LOWER],0) as GREASE_LOWER,isnull(p.[SPACER_MEASURE], d.Spacer_Measure) as SPACER,isnull([QTY_SPACER_CHANGE],0) as 'CHANGES', " &
                                                                                              "isnull([LATERAL_DATE_TIME],'1999-11-11 11:11:11.111') as LATERAL_DATE_TIME, " &
                                                                                              "isnull([GREASE_DATE_TIME],'1999-11-11 11:11:11.111') as GREASE_DATE_TIME, isnull([LINE_NUM],d.linenum) as LINE, " &
                                                                                              "isnull(BAD_PART, 'BAD_INFO') as Result, isnull([AIR_PRESSURE1], 0) as PRESSURE " &
                                                                                              "FROM [NB_ASSEMBLY].[dbo].[PieceData] p full outer JOIN New_Assembly_Access.dbo.Tbl_Data AS d " &
                                                                                              "ON substring(d.zbarcode,3,6) = right(p.serial_number,6) and month(d.cup_mfg_date) = month(p.cup_date) and " &
                                                                                              "right(d.zbarcode,10) = right(p.cup_part_num,10) join new_assembly_Access.dbo.tbl_job j on j.job# = d.job# where d.job# = @Job2", con)


                                                                    sql.Parameters.AddWithValue("@Job2", job)
                                                                    da.SelectCommand = sql

                                                                    Dim dt As New DataTable
                                                                    da.Fill(dt)
                                                                    Invoke(Sub()
                                                                               dgvJob.DataContext = dt.DefaultView
                                                                               dgvJob.AutoGenerateColumns = True
                                                                               dgvJob.CanUserAddRows = False
                                                                           End Sub)


                                                                End Using


                                                            End Sub) With {
        .IsBackground = True
                                        }
    bgThread.Start()

End Sub

Public Sub Button_Click(sender As Object, e As RoutedEventArgs)

    txtJob.IsEnabled = False
    btnEnter.IsEnabled = False
    job = txtJob.Text

    ReadDatabase()

End Sub

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    txtJob.Focus()
End Sub

我建议你稍微清理一下你的代码并制作只做一件事的方法 - 不要有一个 ReadDatabase 也摆弄控件等

DataAdapter 可以接受一个 sql 字符串和一个连接字符串,它知道如何建立连接并打开它等。它整理一切以将所有这些交给 DA

也许将那个巨大的 SQL 字符串放入资源文件

Private Async Function GetComponentsDataTable() as Task(Of DataTable)

    Dim con As String = My.Settings.New_Assembly_AccessConnectionString
    Dim sql as String = "SELECT isnull(p.[CLASS],j.class) as 'CLASS',isnull([SERIAL_NUMBER],left(year(d.cup_mfg_date),2) + d.cup_serial) as SERIAL, " & _
                                      "isnull([CUP_DATE],d.cup_mfg_date) as CUPDATE, isnull([CUP_PART_NUM],left([Cup#],charindex('-',Cup#)-1)) as PARTNUM, isnull(p.[LATERAL_VALUE], " & _
                                      "d.lateral_value * 10000) as LATVALUE, isnull([LAT_UPPER],0) as LAT_UPPER,isnull([LAT_LOWER],0) as LAT_LOWER, " & _
                                      "isnull(p.[BEFORE_WEIGHT],0) as BEFORE_WEIGHT, isnull(p.[AFTER_WEIGHT],0) as AFTER_WEIGHT,isnull([GREASE_UPPER],0) as GREASE_UPPER, " & _
                                      "isnull([GREASE_LOWER],0) as GREASE_LOWER,isnull(p.[SPACER_MEASURE], d.Spacer_Measure) as SPACER,isnull([QTY_SPACER_CHANGE],0) as 'CHANGES', " & _
                                      "isnull([LATERAL_DATE_TIME],'1999-11-11 11:11:11.111') as LATERAL_DATE_TIME, " & _
                                      "isnull([GREASE_DATE_TIME],'1999-11-11 11:11:11.111') as GREASE_DATE_TIME, isnull([LINE_NUM],d.linenum) as LINE, " & _
                                      "isnull(BAD_PART, 'BAD_INFO') as Result, isnull([AIR_PRESSURE1], 0) as PRESSURE " & _
                                      "FROM [NB_ASSEMBLY].[dbo].[PieceData] p full outer JOIN New_Assembly_Access.dbo.Tbl_Data AS d " & _
                                      "ON substring(d.zbarcode,3,6) = right(p.serial_number,6) and month(d.cup_mfg_date) = month(p.cup_date) and " & _
                                      "right(d.zbarcode,10) = right(p.cup_part_num,10) join new_assembly_Access.dbo.tbl_job j on j.job# = d.job# where d.job# = @Job2"

    Using da as New SqlDataAdapter(sql, con)

        da.SelectCommand.Parameters.AddWithValue("@Job2", job) 'see https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/'
        Dim dt As New DataTable
        Await Task.Run(Sub() da.Fill(dt))           'also, see note from AlexB
        Return dt
    End Using

End Sub

Public Async Sub Button_Click(sender As Object, e As RoutedEventArgs)

    txtJob.IsEnabled = False
    btnEnter.IsEnabled = False


    Dim dt = Await GetComponentsDataTable()

    dgvJob.AutoGenerateColumns = True
    dgvJob.DataContext = dt.DefaultView
    dgvJob.CanUserAddRows = False

End Sub

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    txtJob.Focus()

End Sub