![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Powershell EventHandling syntax with SQL Server? How do I add a script block as an event handler? In C# I can use the following: SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;INtegrated Security=True"); conn.FireInfoMessageEventOnUserErrors = true; conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage); When I tried to run this version: $SQLConnection = new-object System.Data.SqlClient.SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=master"); $SQLConnection.Open() $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write- Host "Hello World"; Write-Host "$($_)"} $SQLConnection += $handler I get the error: Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'. At H:\a\sqlevent.ps1:10 char:18 + $SQLConnection += <<<< $handler Any help or links on how to do this would be vastly appreciated! Thanks, --Chris |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Powershell EventHandling syntax with SQL Server? christhorsen@xxxxxx wrote: Quote: > How do I add a script block as an event handler? > > In C# I can use the following: > > SqlConnection conn = new SqlConnection("Data Source=.;Initial > Catalog=master;INtegrated Security=True"); > conn.FireInfoMessageEventOnUserErrors = true; > conn.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage); > > When I tried to run this version: > > $SQLConnection = new-object System.Data.SqlClient.SqlConnection("Data > Source=.;Integrated Security=SSPI;Initial Catalog=master"); > $SQLConnection.Open() > $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write- > Host "Hello World"; Write-Host "$($_)"} > $SQLConnection += $handler $SQLConnection.add_InfoMessage($handler) I just used get-member to try to figure out the syntax, but wasn't able to actually test it out. Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Powershell EventHandling syntax with SQL Server? Marco, That worked well. The behavior is a bit different than in C# because the Powershell output from multiple raiserror (with no_wait) statements all arrives in one call to the handler In C# the function would be called once per raiserror. It's still way better than what I had. Thanks, --Chris |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| VBScripter trying to understand PowerShell syntax | PowerShell | |||
| Syntax problem with powershell and variables | PowerShell | |||
| Help with Powershell syntax | PowerShell | |||
| Powershell syntax highlighting for SciTE? | PowerShell | |||
| Powershell.exe command-line syntax | PowerShell | |||