C#: string or String?

Question

What’s the deal with the string and other types in C#? I see both string and String all over the place. Same goes with int and Int32 and other types.

For example, I see both:

string text = "Some Text Here";

String text = "Some More Text Here";

What is the difference between them?

Short Answer

None. There is no difference between them. Use the one you like best.

Really? No difference whatsoever?
Why have both forms then? What’s the point?

Well…

Continue reading “C#: string or String?”

Advertisements

SQL Trace And The Stream Of Query Plans

In 8 Ways To Hunt T-SQL Query Plans, we looked at some simple ways to get our hands on those valuable query plans. However, sometimes we might want to record query plans as part of a bigger effort to profile and troubleshoot a server, or perhaps optimize it for a given workload.

When this need arises, there are two additional tools we can make use of:

  • SQL Trace
  • Extended Events

In this pumpkin we’ll take a look at how we can use SQL Trace to accomplish this goal.

Continue reading “SQL Trace And The Stream Of Query Plans”

How To Edit The Windows Registry In PowerShell

Question

How can I access the Windows Registry in PowerShell so I can create, update and delete keys and values?

Short Answer

Use the Get-ItemProperty and Set-ItemProperty Cmdlets:

# get the data from a specific registry key value
Get-ItemProperty "HKLM:\SOFTWARE\MyRegistryKey" |
    % { $_.MyKeyValue };

# set the data for a specific registry key value
Set-ItemProperty "HKLM:\SOFTWARE\MyRegistryKey" -Name "MyKeyValue" -Value "Some Data";

Long Answer

When playing with the Windows Registry, the common Item Cmdlets are our friends:

Continue reading “How To Edit The Windows Registry In PowerShell”

How To Gracefully Stop An Asynchronous Job In PowerShell

Question

How can we ask an asynchronous PowerShell job to gracefully stop on its own?

Short Answer

We can use a marker file pattern for that effect:

# this is the path to the marker file
$JobStopMarkerFile = "C:\Temp\JobStopMarker.txt";

# ensure the marker file does not exist
if (Test-Path -LiteralPath $JobStopMarkerFile)
{
    Remove-Item $JobStopMarkerFile
}

# this contains the script to run async
$JobScript = {

    param
    (
        [Parameter(Mandatory = $true)]
        $JobStopMarkerFile
    )

    # this flag will be set when the job is done
    $IsJobDone = $false

    # this condition checks whether
    # 1) the job is done
    # 2) the job has been asked to stop
    while (-not $IsJobDone)
    {
        # some recurring task would run here instead
        Start-Sleep -Seconds 5

        # uncomment and set this whenever the job finishes on its own
        # $IsJobDone = $true

        # check if the marker file exists
        if (Test-Path -LiteralPath $JobStopMarkerFile)
        {
            # signal the job as completed
            $IsJobDone = $true;

            # cleanup the file too
            Remove-Item -LiteralPath $JobStopMarkerFile
        }
    }
}

# start the job now
$Job = Start-Job -ScriptBlock $JobScript -ArgumentList $JobStopMarkerFile

# do some other stuff here
Start-Sleep 1

# ask the job to stop
# we do this by *creating* an empty marker file
# the job itself will remove this file
$null > $JobStopMarkerFile

# wait for the job to finish
Wait-Job $Job

There is quite some code here, isn’t there? So how does it work?

Long Answer

If we just want to stop a job, minus the gracefully bit, we can simply use the Stop-job Cmdlet:

Continue reading “How To Gracefully Stop An Asynchronous Job In PowerShell”

How To Discover The Installed Version Of PowerShell

Question

How can we discover which version of PowerShell we are running on?

Quick Answer

We can use the $PSVersionTable system variable:

$PSVersionTable.PSVersion

This will return an object with the installed version build numbers:

PowerShell Installed Version

Long Answer

We can use $PSVersionTable to discover the versions of different components of PowerShell:

Continue reading “How To Discover The Installed Version Of PowerShell”

How To Uninstall An Application In PowerShell

Question

How can we uninstall an application listed in Programs and Features using PowerShell?

Short Answer

We can use the Win32_Product WMI class.

$app = Get-WmiObject
    -Class Win32_Product
    -Filter "Name = 'Application Name'"
$app.Uninstall()

Long Answer

We can use the Win32_Product WMI class to query the system for installed applications.

Continue reading “How To Uninstall An Application In PowerShell”

8 Ways To Hunt T-SQL Query Plans

In How Can Query Plans Help Us? we looked at how query plans can be made to help us troubleshoot a runaway query. Then in Are Query Plans All We Need? we looked at other tools we need to keep ready in our performance tuning utility belt. With that in mind, it is now time to understand how we can capture query plans to begin with.

So Where Do We Start?

To understand how we can capture query plans, we must first understand what types of query plans are there to capture. Luckily, there is only two of them.

Continue reading “8 Ways To Hunt T-SQL Query Plans”

Are Query Plans All We Need?

In How Can Query Plans Help Us?, we looked at the nature of T-SQL and then at some of the benefits that understanding query plans can bring us. But are query plans a silver bullet? Can we overcome all optimization challenges with query plans only or are there other tools we need on our utility belt?

Continue reading “Are Query Plans All We Need?”

How Can Query Plans Help Us?

In T-SQL Query Plan Analysis Prelude we looked at some common reasons why queries can sometimes go on a snail speed contest and that query plans are one of the ways we can know how to transform those carefree molluscs into caffeinated cheetahs.

So how exactly can query plans help us do that? Are they like magic wands that turn over-ripe pumpkins into dubious-expiry carriages and make used teeth disappear in return for equally used pennies?

Well… Hold that thought for a minute.

Continue reading “How Can Query Plans Help Us?”

What Is MDX?

In the previous pumpkin I put down what I intend to learn during this exploration of MDX and how I’m going to go about it. But what does MDX mean, anyway? And where did it come from? And how is it used? And for what purpose?

Continue reading “What Is MDX?”