Thursday, January 3, 2008

Microsoft SQL, stored procedures, optional parameters

Here is a good article on the pros/cons and performance considerations of using optional parameters in SQL stored procedures:

http://www.sommarskog.se/dyn-search.html

Calling Web Services from SQL 2005

Here is an article on how to call Web Services from SQL 2005. The article addresses SQL triggers, but the same approach can be used for user-defined functions or stored procedures.

http://www.codeproject.com/KB/database/SQLCLR.aspx

Monday, December 10, 2007

Powershell Cmdlet and Alias listing

Generate a list of PowerShell Cmdlets:

"Name`tSynopsis`tDescription`tFile"
ls -recurse $PSHOME *-Help.xml | foreach {
$fileName = $_.Name
$help = [xml](gc $_.fullName)
$help.helpitems.command | foreach {
write-output ([string]::format("{0}`t{1}`t{2}`t{3}",$_.details.name.trim(),$_.details.description.get_InnerText().trim(),$_.description.get_InnerText().trim().replace("`n", " "),$fileName))
}
}

Generate a list of Aliases:

"Name`tDefinition"
get-alias | sort -property Name | select Name, Definition |% {
write-output ([string]::format("{0}`t{1}", $_.Name,$_.Definition))
}

Each output is tab delimited.

Sunday, December 2, 2007

Generate .NET Guid

To generate a .NET guid in PowerShell:

Write-Host ([System.Guid]::NewGuid())

Or to generate a bunch of guids:

1..30 |% { Write-Host ([System.Guid]::NewGuid()) }

Counting # of matching lines in file

I wanted to scan a large number XML files and determine which ones had more than 20 elements of a particular type. Here was the PowerShell command-line I used to report on files that contains more than 20 "" elements:

gci *.xml |% {$fn=$_.name; gc $_ | where {$_ -match ""} |% {$count = 0}{++$count}{if ($count -gt 20) {$fn}}}

An easier, but not very good for performance if a large number of files are involved is:

gci f_*.xml | select-string "" | group filename | where {$_.count -gt 20} | select count, name

Tuesday, November 6, 2007

PowerShell to report top 10 results from log file

Ever want to sift through a log file and report on the top 10 occurances of a certain field value? Here is a PowerShell script that will do just that.

I had a log file that contained an IP address in the third field (column index 2 since arrays start at zero in PowerShell). I wanted to know what were the top 10 IPs that were logs. I could call this script like:

./ipcount.ps1 logfile.log 2

When using huge log files, don't forget if you want to redisplay, but not recompute, the results, you can "dot source" the script like:

. ./ipcount.ps1 logfile.log 2

Then $result will always hold the last set of results. Here is the script (3 lines...middle line is really long):

param($file,$index)

$result = gc $file | foreach {$hash=@{}}{$hash[$_.split(',')[[int]$index]] += 1}{$hash.getenumerator()} | sort value -desc | select -first 10

$result

Friday, November 2, 2007

URLs and System.IO.Path

Cool. I just found out that the .NET System.IO.Path static methods like GetFilename and GetDirectory work with internet URLs as well. I was wanting to get just the file name from a URL and the System.Uri class doesn't do this...but Path does. Now ain't that slick.

Friday, October 26, 2007

Finally...a use for .NET partial classes

When .NET 2.0 was released, partial classes was recognized as one of the top four cool new features (along with generics, anonymous methods, and iterators). After developing with 2.0 for some time now, I haven’t found a good reason to use partial classes (besides the Page class in ASP.NET that is created for you automatically). I do however recognize its importance for Visual Studio 2005 and how the auto-generated code and our own Page classes are created. Besides that, I just haven’t ran into a situation where I really needed it.

Recently I wrote some code where I stood up and cheered partial classes. If you ever used the .NET xsd.exe utility in .NET 1.1 to create a strong-typed class for an XSD file, you know that the tool created this handy set of classes for you to access the elements of an XML file. This was a good thing, but I would always end up writing several separate utility classes to provide a layer on top of, or along side of, the auto-generated class from xsd.exe. Although tempted at times, I couldn’t modify the auto-generated class file since I might regenerate it at a later time (I always did).

Much to my glee, xsd.exe in .NET 2.0 now creates partial classes! This now allows me to write separate class files and add properties and methods to the auto-generated ones from xsd.exe. This provides a nice separation/coupling from the xsd.exe generated code. Excellent!

I see a pattern here…for things that auto-generate code, partial classes are a welcome addition to .NET.

Friday, October 19, 2007

DataFormatString not working in GridView

I often use the DataFormatString in the .NET 2.0 GridView control. It seems like the DataFormatString property on a bound column is ignored unless you also set the column's HtmlEncode property to false (the default value is true).

.NET ToString() formatting help

Here is a useful 2-page cheat sheet on the various ToString() formatting options in the .NET Framework languages.

Download Here

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...