C# to PowerShell Translation Thought Process
A gentleman on the powershell news group the other day was asking about executing SQL stored procedures and I provided him with example that I had posted earlier in my blog (Click Here) While I’m not sure my answer was exactly what he was looking for he was a little curious as to how I went about translating the original C# code. To be clear I am not a coder nor do I truly know C#, but I know enough to translate. Anyway, I decided this would be a good idea for a post
So… Here we GO!
First… I used this as the C# example
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx
I will show you my thought process by section I will include my comments and after I will post Both code Sections
This was fairly simple. In C# you have the ability to take namespace shortcuts by using ‘using
The C# Code:
conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
conn.Open();
The Powershell Code:
$db = "Northwind"
$conn = new-Object System.Data.SqlClient.SqlConnection("Server=$srv;DataBase=$db;IntegratedSecurity=SSPI")
$conn.Open() | out-null # The out-null is because the method returns a value and I dont want that output
Again… I had to find out what SqlCommand was referencing so… back to MSDN… System.Data.SqlClient.SqlCommand. BTW… I think I should take time now to tell you it is a REALLY GOOD idea to get use to the idea of constructors (how the object should be created) and how to use MSDN to determine the correct way to create an instance of the class/object. It really helps to know what a class is expecting. In this example its good to know the constructor is wanting a string of the SP and A connection OBJECT to use.
The C# Code:
// the stored procedure
SqlCommand cmd = new SqlCommand("CustOrderHist", conn);
Here is the PowerShell Code:
Here was the tricky part (at least sorta.) From the C# code its not clear if the CommandType.StoredProcedure is a property and it turns out its not. It is an enumeration. It took me a few clicks to figure it out. The first clue was when looking for CommandType… I got an enum and it turns out the valid options was StoredProcedure, TableDirect, or Text (MSDN LINK) Clearly this was an enum, but I was a little unsure how do to enums in powershell. I found this blog by /\/\0\/\/ that helped a lot (/\/\o\/\/ Link)
The C# Code:
// to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
I ended up with this Powershell Line:
This was fairly simple as well… Just had to drop the new sqlparameter because strongly typing was not required. Again the out-null was because the method returns data I did not want as well as set the parameters.
The C# Code:
// will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@CustomerID", custId));
The PowerShell Code:
These two parts are really just the excution of the the previous code. I think the only difference is the way PowerShell Writes output
The C# Code:
rdr = cmd.ExecuteReader();
// iterate through results, printing each to console
while (rdr.Read())
{
Console.WriteLine(
"Product: {0,-35} Total: {1,2}",
rdr["ProductName"],
rdr["Total"]);
}
The Powershell Code:
While($rdr.Read()){
Write-Host "Product Name: " $rdr[‘ProductName’]
Write-Host "Total: " $rdr[‘Total’]
}
Here is the Complete PowerShell Code.
$db = "Northwind"
$conn = new-Object System.Data.SqlClient.SqlConnection("Server=$srv1;DataBase=$db;Integrated Security=SSPI")
$conn.Open() | out-null
$cmd = new-Object System.Data.SqlClient.SqlCommand("CustOrderHist", $conn)
$cmd.CommandType = [System.Data.CommandType]‘StoredProcedure’
$cmd.Parameters.Add("@CustomerID","ANATR") | out-Null
$rdr = $cmd.ExecuteReader()
While($rdr.Read()){
Write-Host "Product Name: " $rdr[‘ProductName’]
Write-Host "Total: " $rdr[‘Total’]
}
$conn.Close()
$rdr.Close()
tshell :: Mar.05.2007 :: .NET, HowTo, Powershell, SQL :: 6 Comments »
6 Responses to “C# to PowerShell Translation Thought Process”
Leave a Reply
You must be logged in to post a comment.


$cmd.CommandType = [System.Data.CommandType]‘StoredProcedure’
You should be able to do it a little easier:
$cmd.CommandType = ‘StoredProcedure’
Does that work for you?
Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Thanks Jeffrey, I could have swore I had tried that before… first actually, but I guess I didn’t because it does work.
The line:
$conn = new-Object System.Data.SqlClient.SqlConnection(
Thanks Thomas… Fixed
[...] As I already did read this post, C# to PowerShell Translation Thought Process , and to make it easier I added C# to my search. [...]
[?] As I already did read this post, C# to PowerShell Translation Thought Process