Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Powershell EventHandling syntax with SQL Server?

Reply
 
Old 12-10-2007   #1 (permalink)
christhorsen


 
 

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 SpecsSystem Spec
Old 12-10-2007   #2 (permalink)
Marco Shaw [MVP]


 
 

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
Give this a try:

$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 SpecsSystem Spec
Old 12-10-2007   #3 (permalink)
christhorsen


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46