/// Frank Hagen: Professional Web Developer, C# User, Reformed Über-geek RSS 2.0
# Wednesday, July 23, 2008
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.
Wednesday, July 23, 2008 10:55:30 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
PowerShell | SQL

Earlier, I wrote about getting data from MS-SQL with PowerShell, which is a very useful and powerful thing.  But just displaying data is very limited.  I have a need at work to create extract "reports" for business owners frequently into Excel.  So I have leverage this scripting technique to do the legwork for me.  After many iterations, I have completed the first version of a script that will take a SQL query in a text file and create an Excel file with the results.  All fully automated with time-stamping, archiving of the successful query, and the ability to export XML or CSV as well.

The script is unfortunately only listed as a page on this site because WordPress does not allow posting of non-media files.  You will have to copy the full content and paste it into a text file named QuerySQL.ps1.  One day, I will host this weblog on a real provider in order to have better control (I will accept donations!).  If many folks prefer, I will post it on my personal webspace with my ISP and provide links on request.  And maybe to CodeProject too.

Feel free to use it in anyway you wish.  Please learn from it if you can, or conversely, send me a note on how to improve it.  I would love to get the field-name bolding working among other things.

Wednesday, July 23, 2008 10:29:00 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
PowerShell | SQL
# Friday, July 18, 2008

Heralded by many to be one of the founding series of modern science fiction, this is the first book of the Lensmen series.  It has been made into games, TV shows, etc, none of which I am familiar with.  So, I thought I'd better get with it and do so.  I picked up an old dime-store copy and got reading.

Triplanetary seems to be a collection of early stories and a novella that sets the stage for the series to come.  The first few stories tell of the fall of Atlantis, the burning of Rome, WWI and WWII, etc.; important events in human history.  They expose an galactic plot between two superpowers at war with one another, although one doesn't know of the others existence, in which Earth and its inhabitants are unknowing pawns.  The final novella is about 3 Terrans captured by space pirates, escape, then by a vastly advance fish alien civilization, which, of course, are able to escape from again.  Fortunately, Terran military scientists are able to quickly reverse engineer the fish alien's vastly superior technology in only a few weeks (days?), and built a super-battleship capable of defeating the aliens very civilization.

Yes, this is early sci-fi.  Pulp fiction, et al.  And I probably would offend the leet geeks, but I thought this book was terrible.  Maybe this was a fan-service book to set the series, but I felt the characters were flat and trite; I didn't care what happened to them at all.  The swooning girl, the incredibly capable hero with a tender spot for our frail damsel, the military leaders with single-minded faith in our hero to the point of having no contingencies, all make for great plot indeed.  Maybe I have been led to expect too much, but Burroughs, Howard, Asimov, Clarke and the others have done so, and in surrounding time frames.  So, I won't find out if the rest of the series is any good.  I just don't care.  Indeed, I can't even be bothered to find cover art for this post.

Friday, July 18, 2008 10:49:02 AM (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