/// Frank Hagen: Professional Web Developer, C# User, Reformed Über-geek RSS 2.0
# Tuesday, October 30, 2007
Here's an extremely useful stub to get data from SQL-Server within a PowerShell script:
$TaskName = "20071029-AllRejectedDuring"
$SqlServer = "SQLDEV01";
$SqlCatalog = "MyData";

# Get the T-SQL Query from .SQL file
$SqlQuery = Get-Content (".\" + $TaskName + ".sql")

#Write-Host ($SqlQuery) -foregroundcolor "gray"

# Setup SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlCatalog; Integrated Security = True"

# Setup SQL Command
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

# Setup .NET SQLAdapter to execute and fill .NET Dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet

#Execute and Get Row Count
$nRecs = $SqlAdapter.Fill($DataSet)

Write-Host ($nRecs.ToString() + " Records retrieved.") -foregroundcolor "Cyan"
$SqlConnection.Close();

if ($nRecs -gt 0)
{
  # Do Stuff
  $DataSet.Tables[0].Rows[0][0]  #Print first data element
}

The connection is using Integrated Security for simplicity, it wouldn't be difficult to switch to UID/PWD instead.  Also, I put the SQL in a .sql file (flat text) to make life easier; you could also put the statement in the string declaration, if it is a simple query.  PowerShell's Get-Content mechanism makes reading a file very easy.  Also, clean up after yourself, I won't include that here.

UPDATE:  I have posted a full script to export to Excel or XML in a followup post.

Tuesday, October 30, 2007 11:51:30 AM (Eastern Standard Time, UTC-05:00)  #    Comments [2] -
PowerShell | SQL
# Monday, October 29, 2007

If you do any scripting at all in Windows, you should be using PowerShell to do it.  But the first time you do, assuming you've installed it properly is the following:

File C:\DEV\Report.ps1 cannot be loaded because the execution of scripts is
 disabled on this system. Please see "get-help about_signing" for more details.

You have two options:  sign your scripts (you should do this) or take the easy route and change the Execution Policy (do this at your own risk).  If you want to do it the right way, see Scott Hanselman's excellent post on the subject.  If you just want to run ps1 scripts, and are very careful about the source, namely yourself, execute the following command within the PowerShell shell:

Set-ExecutionPolicy RemoteSigned
Monday, October 29, 2007 1:12:35 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
PowerShell
# Friday, October 26, 2007

Here's your reminder that DST is ending again this weekend and you will be losing your free hour everyday.  But Wait!!  Congress moved it this year:  It's actually next week!  Yay!  Another week of free hours.  It a good thing Congress is saving us Time and Money, and more than ever before!

StandardTime.com

Friday, October 26, 2007 11:00:27 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Life | Rant | Worse Than Failure
# Wednesday, October 24, 2007

Tiny milestone:  1000 page views at this site!

The number of views to Using since moving to WordPress has exceeded 1000 today.  woot...

Wednesday, October 24, 2007 2:47:44 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Blog
# Monday, October 22, 2007

Bill Mauldin is probably the most famous cartoonist from World War II.  He was an infantry soldier in the Italian campaign who also worked for the Italian theaters version of Stars and Stripes, the soldier-run newspaper.  If you were to see one of his strips, you would immediately recognize his work.

The Brass Ring is Mauldin's autobiography of his early life through the end of WWII.  He tells of his very humble beginnings as the son of a poor farm family, life in the depression, and the start of his career as an illustrator.  He joined the National Guard at an early age at the encouragement of a close friend as it became evident that the Guard would be Federalized at the beginning of the war and before he could be drafted.  He was able to quickly establish himself as a journalist and cartoonist and so avoiding direct insertion into a combat unit.  This is the story of his experiences and the material he created from them.

The Brass Ring has a much more linear telling than Up Front and is easier to read because of it.  Again, I enjoyed the perspective of the infantryman in the trenches although Mauldin never really experienced combat as a reporter.  He seemed to be willing to put himself in the thick of it though, which is refreshing for a rear echelon type.  An enjoyable book, but not the collection of his work that I have been hoping for.  I will keep trying.

Monday, October 22, 2007 11:50:02 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Books
# Tuesday, October 16, 2007

I shouldn't be surprised, but I am:  A customer complained to me that the Flag field they wanted to indicate a Yes or No value was failing.  The Yes value was working fine, but No was being returned when they hadn't set anything yet.

I didn't realize that booleans were meant to represent Yes, No, and Maybe.  Silly Programmer, no bits for you.

Tuesday, October 16, 2007 10:43:53 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Worse Than Failure
# Tuesday, October 09, 2007

I am struggling with issues posting code samples in WordPress.  I am painfully aware that much of my code is cut off visually on the main page, and highlighting is spotty at best.  Suggestions for making it work in WordPress are appreciated.  I'd love to add an external stylesheet, but am loathe to pay for that add on.  An "extra" to handle it would be great, as long as it doesn't violate any EULAs.

Of course, WP layouts are pretty lacking too, without purchasing additional capabilities.  If I wanted to spend money doing this, I'd host this blog myself somewhere else and have Ultimate Freedom

I am working on it....

Update [10.08]:  I will be looking at Blogger and Blogsome to evaluate their engines for free coding blogs.  It is possible I may move again.  Also, I will renew my feedburner account so moves are more transparent; or completely so for RSS readers.

Tuesday, October 09, 2007 9:44:55 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Blog

This really should have been more obvious to me, so why did I have to look it up?  If you need to check for NULL in a returned field from SQL (or any other datasource), you should the .Equals method on System.DBNull.Value.  I usually prefer the "==" notation for conditionals, but that's just me.  This is more efficient.  The code follows:

if (!dsSpecQuery.Tables[0].Rows[0]["device_type_end"].Equals(System.DBNull.Value)) 
{
    _EndDate = Convert.ToDateTime(dsSpecQuery.Tables[0].Rows[0]["device_type_end"]);
}

Of course, dsSpecQuery is a DataSet, and the field in question is DateTime out of SQLServer (not that it matters for the conditional).

Tuesday, October 09, 2007 9:38:06 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
.NET | Programming | SQL
# Thursday, October 04, 2007

Lord Foul's Bane is a fantasy classic that I have read before; twice, I believe. But it was so long ago, that I have been meaning to re-read it for awhile.  The books I have are so old, Amazon doesn't even have the images to link in.  Hopefully Wikipedia doesn't mind.

Lord Foul's Bane is the beginning of the story of Thomas Covenant called Unbeliever.  He is a modern day man afflicted by Leprosy.  He finds himself unwillingly thrust into The Land, a mystical realm that is able to resolve some of his nerve damage and remove his disease.  But he is wary, knowing it must be too good to be true.  He must deliver a message of Doom to the leaders of the Land, and resolve his inner turmoil of even being there.

Covenant is a reluctant hero with fears and flaws that match our own.  Donaldson's treatment of him is very deep and extremely satisfying when looking for a character that is more realistically complex.  He spends much of this book battling the psyche of the main character himself.  There are others, I am certain, that have written this series up far better than I.  For more, find such a review online.  This is the first book of the first trilogy of the Chronicles of the Unbeliever.  The 2 trilogies were so popular that Donaldson has started the 3rd and final a couple years ago.  The Chronicles of Thomas Covenant are, without a doubt, required reading for any fantasy fan.

Thursday, October 04, 2007 1:23:00 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Books
<%--
--%>
Statistics
Total Posts: 186
This Year: 0
This Month: 0
This Week: 0
Comments: 72
Locations of visitors to this page
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Frank W Hagen
Sign In
All Content © 2010, Frank W Hagen
Custom DasBlog theme based on 'Business' by Christoph De Baene