0

I'm trying to make my SQL commands running asynchronously with PowerShell. To achive this i'm using SqlCommand class and BeginExecuteNonQuery method. As far as i know, this method should return IAsyncResult class. But when I'm trying to inspect returning value i see System.Data.Common.DbAsyncResult class.

Please tell me what i'm doing wrong or where i can read about my mistake, i'm quite new to programming. My code sample is below

$connection = New-Object System.Data.SqlClient.SqlConnection
"addr=tcp:192.168.0.81,1433;Database=ArchiCredit;uid=sa;password=1;Async=true;"
$connection.Open()
$connection.State
$command = New-Object System.Data.SqlClient.SqlCommand ("update clients set fullname = 'ololo' where id = 15", $connection)
$state = $command.BeginExecuteNonQuery()
$state | gm

result of this script is

PS C:\Users\ivanov> F:\AsyncSQL.ps1
Open


TypeName: System.Data.Common.DbAsyncResult

Name        MemberType Definition                    
----        ---------- ----------                    
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()             
GetType     Method     type GetType()                
ToString    Method     string ToString()
KennyMacCormik
  • 105
  • 2
  • 10

1 Answers1

0

That is normal. IAsyncResult is an interface. A class implements an interface, and anyone who uses that class is guaranteed that class will have all the properties/methods specified by the interface. In this case I believe DbAsyncResult is an internal class. Maybe that's why PowerShell isn't showing you the the properties. Strange.

Despite not seeing its properties, you can wait for it like this:

$state = $command.BeginExecuteNonQuery()
while( -not $state.IsCompleted )
{
    Start-Sleep -Milliseconds 100
}
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91