Я рекомендую вам немного очистить свой код и создать методы, которые делают только одно - не иметь ReadDatabase, которая также возится с элементами управления, et c
DataAdapter может принимать строку sql и строку подключения, он знает, как установить соединение и открыть его et c. Он приводит все в порядок, чтобы передать все это 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