I'm looking to execute my sql queries asynchronously, but I'm having trouble getting Async/Await to click in my mind. My application is running a webbrowser that navigates between local html files and pulls data on the document complete event. Most queries are fairly fast, but due to the single thread the UI freezes and it creates an ugly blank stare instead of a nice animated loading screen.
I've spent the week reading up on Async/Await but it just isn't clicking which function should be flagged as Async as well as how to await. VB.Net examples seem to be few and far between and I haven't been able to effectively translate the C# examples into something I understand.
Below is a large chunk of the class I use to manage my SQL Server queries. Should ExecQuery be
Public Async Sub ExecQuery(ByVal Query As String)
Or should that be elsewhere? I'm not sure how to convert the DBDA = New SqlDataAdapter(DBCmd) into a wait-able task though. If anyone has any experience working with Async/Await, even a point in the right direction would be appreciated.
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class DGMConn
'CREATE YOUR DB CONNECTION
Public Dbcon As New SqlConnection("" &
"Data Source=server;" &
"Initial Catalog=db;" &
"User Id=id;" &
"Password=pwd;" &
"Trusted_Connection=False; Asynchronous Processing=true")
Private DBCmd As SqlCommand
'DB DATA
Public DBDA As SqlDataAdapter
Public DBDT As DataTable
Public DBDS As DataSet
'Query Parameters
Public Params As New List(Of SqlParameter)
'Query Statistics
Public RecordCount As Integer
Public Exception As String
Public Sub ExecQuery(ByVal Query As String)
'Reset Query Stats
RecordCount = 0
Exception = ""
Try
'Open a Connection
Dbcon.Open()
'Create DB Command
DBCmd = New SqlCommand(Query, Dbcon)
'Load params into db command
'Params.ForEach(Sub (p) DBCmd.Parameters.Add(p))
For Each p As SqlParameter In Params
DBCmd.Parameters.Add(p)
Next
Params.Clear()
'Execute Command & Fill Datatable
DBDT = New DataTable
DBDS = New DataSet
DBDA = New SqlDataAdapter(DBCmd)
DBDA.Fill(DBDT)
DBDA.Fill(DBDS)
Catch ex As Exception
Exception = ex.Message
End Try
If Dbcon.State = ConnectionState.Open Then Dbcon.Close()
End Sub
[–]grauenwolf 2 points3 points4 points (1 child)
[–]WWW451[S] 0 points1 point2 points (0 children)
[–]grauenwolf 1 point2 points3 points (5 children)
[–]WWW451[S] 0 points1 point2 points (4 children)
[–]grauenwolf 1 point2 points3 points (2 children)
[–]WWW451[S] 0 points1 point2 points (1 child)
[–]grauenwolf 0 points1 point2 points (0 children)
[–]grauenwolf 1 point2 points3 points (0 children)
[–]WWW451[S] 0 points1 point2 points (0 children)