Things I've Learned

  • ADO.NET in PowerShell...Update SQL Data Example/Sample

    We have a database that stores all our configuration key/value pairs in various tables (1 per component). This code updates the table based on the specifed table, key, and value.

     

    # Open SQL connection

    $sqlConnection = New-Object System.Data.SqlClient.SqlConnection

    $sqlConnection.ConnectionString = "Server=$computer;Database=ConfigurationStore;Integrated Security=True"

    $sqlConnection.Open()

     

    # Get DataTable to modify

    $sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter

    $dataTable = New-Object System.Data.DataTable

    $sqlCommandSelect = New-Object System.Data.SqlClient.SqlCommand

    $sqlCommandSelect.CommandText = "SELECT * FROM $table WHERE Name='$key'"

    $sqlCommandSelect.Connection = $sqlConnection

    $sqlDataAdapter.SelectCommand = $sqlCommandSelect

    $sqlDataAdapter.Fill($dataTable)

     

    # Setup UpdateCommand

    $sqlCommandUpdate = New-Object System.Data.SqlClient.SqlCommand

    #$sqlCommandUpdate.CommandText = "UPDATE dbo.$table SET $table.Value = N'$value' WHERE Name = N'$key'"

    $sqlCommandUpdate.CommandText = "UPDATE dbo.$table SET $table.Value = @Value WHERE Name = @Name"

    $sqlCommandUpdate.Connection = $sqlConnection

    $sqlDataAdapter.UpdateCommand = $sqlCommandUpdate

    $sqlDataAdapter.UpdateCommand.Parameters.Add("@Value", [System.Data.SqlDbType]::NVarChar, 500, "Value")

    $sqlParameter = New-Object System.Data.SqlClient.SqlParameter

    $sqlParameter = $sqlDataAdapter.UpdateCommand.Parameters.Add("@Name", [System.Data.SqlDbType]::NVarChar)

    $sqlParameter.SourceColumn = "Name"

    $sqlParameter.SourceVersion = [System.Data.DataRowVersion]::Original

     

    # Update DataTable

    $dataTable.Rows[0].Value = $value

    $sqlDataAdapter.Update($dataTable)

    $sqlConnection.Close()

  • Creating a folder/file name based on a date time stamp in NT Shell Script (Batch)

    for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datetimefolder=%%k%%i%%j

    for /F "tokens=1,2,3 delims=: " %%i in ('time /t') do set datetimefolder=%datetimefolder%_%%i%%j%%k

    echo DateTimeFolder=%datetimefolder%

    will output something like:

    20060516_1216PM

  • errorlevel in for loop returns exit code of iterating statement

    It turns out that errorlevel is returning the exit code of iterating statement in the for loop instead of the one immediately preceding it.

    for /r \\computer\share\x86 %%x in (*.xml) do (

        IsVarMap.exe /file:"%%x"

        echo %ERRORLEVEL%

    )

    Echoes 0 always. I expect some iterations to return 2, when xml file isn’t a VarMap.

    IsVarMap.exe /file:notavarmap.xml

    echo %ERRORLEVEL%

    Echoes 2 as expected

  • Notepad

    Sometimes you may click the Edit menu and find that the Go To command is not available. If this is the case, you have probably turned on the 'word wrap' feature. This feature enables you to see all the text on the line, but does not affect the way the text appears when it is printed. If you want to use the Go To command, you need to turn of 'word wrap'. Within Notepad, click the Format menu and remove the check beside Word Wrap.
  • Command Prompt Tips and Tricks

    doskey.exe can be used to create macros for your custom command prompt. Just create a shortcut like this:

    %SystemRoot%\system32\cmd.exe /k doskey /macrofile="C:\tools\DosKeyMacroFile.txt"

    DosKeyMacroFile.txt looks like this:

    [cmd.exe]
        n=notepad.exe $*
        s=pushd C:\scratch
        re=regedit.exe
        reboot=shutdown.exe -r -f -c "rebooting"
        dk=doskey /macros:all
        em=notepad "C:\tools\DosKeyMacroFile.txt"
        tools=pushd "C:\tools"

    As you can see by the first line, you can use $* to specify that this command should receive it's parameters from the command line. For example, if I want to edit test.cmd I just run "n test.cmd" from my command prompt.


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker