Monday, October 28, 2013

Convert a DataTable Column into String Array

Suppose there is a single column DataTable with user names. In order to convert the data in this column into a string array you can do it in for-loop and LINQ approaches.


For-Loop approach:

int columnIndex = 0; //single-column DataTable
string[] userArr = new string[dt.Rows.Count];
for( int i = 0; i < dt.Rows.Count; i++)
{
    userArr[i] = dt.Rows[i][columnIndex].ToString();
}


LINQ approach 1:

int columnIndex = 0; // single-column DataTable
var userArr = dt.Rows.Cast()
                     .Select(row => row[columnIndex].ToString())
                     .ToArray();


LINQ approach 2:

int columnIndex = 0; // single-column DataTable
List userList = new List();
foreach( DataRow row in dt.Rows )
{
    userList.Add( row[columnIndex].ToString() );
}



How to check CentOS version

In order to check current version of CentOS, try the following.

>  cat /etc/*release*

You will see the version similar to the following.

CentOS release 6.4 (Final)
LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
cat: /etc/lsb-release.d: Is a directory
CentOS release 6.4 (Final)
CentOS release 6.4 (Final)
cpe:/o:centos:linux:6:GA


Or more specifically...

> cat /etc/centos-release

This will show just the version number as follows.

CentOS release 6.4 (Final)



Thursday, October 24, 2013

Cannot find server certificate with thumbprint error ... TDE error

Situation 

  1. Primary SQL Server's database has TDE (Transparent Data Encryption) enabled and encrypted with Master key (private key). 
  2. The database backup has been made and backup file was copied to a secondary SQL Server. 
  3. When restoring the database on a secondary SQL Server, "Cannot find server certificate with thumbprint error" happens.
Solution:

1. Backup the certificate with master key on the Primary Server
  • BACKUP CERTIFICATE  [EncryptionCertificate]
    TO FILE = 'Certificate File path'
    WITH PRIVATE KEY (FILE = 'Master Key File path.dat', ENCRYPTION BY PASSWORD ='password')

2. Restore the certificate with master key password on the Secondary Server
  • CREATE CERTIFICATE [EncryptionCertificate]
    FROM FILE='Certificate File path'
    WITH PRIVATE KEY ( FILE =  'Master Key File path.dat' , DECRYPTION BY PASSWORD ='password')

To find a file in Linux by name

For example, to find a file "pg_hba.conf" in CentOS


Method 1
>  cd /
>  sudo find . -depth | grep -i pg_hba.conf


Method 2
> cd /
>  sudo updatedb
>  sudo locate pg_hba.conf



To check if postgresql is running

To check if PostgreSQL service is running on Linux on CentOS:


/sbin/service postgresql status

or

/sbin/init.d/postgresql status

or

ps | grep -i pgsql



Friday, August 16, 2013

PowerShell - Copy files that contain square brackets in file name.

I tried to copy all mp3 files in iTunes music folder's subfolders onto another folder without subfolder structures. This was relatively straightforward task until I ran into files that contain square brackets in file names (i.e., "Tom's Diner [1990].mp3").  For example,

$folderItems = Get-ChildItem "C:\Users\John Doe\Music\iTunes\iTunes media\Music\" -Recurse
$newFolder = "C:\Users\John Doe\My Documents\MusicTemp\"
$foreach( $item in $folderItems )
{
    if( $item.Attributes -eq "Archive" )
    {
        Copy-Item $item.FullName $newFolder
    }
}

The code above kept failing due to PowerShell's de-escaping the strings multiple times internally and using special characters for pattern matching. Getting PowerShell to correctly recognize a literal square bracket in a path string turned out to be more complex than I thought.

I googled around and found a suitable workaround for this problem. Basically you would have to escape both open-square-bracket "[" and close-square-bracket "]" with backtick characters "`".

When using single-quote for string, two backticks are needed in front of a bracket in the string.
For double-quoted string, four backticks are needed in front of a bracket in the string.

The workaround PowerShell script is show below. I used double backticks to properly escape the square brackets.

$folderItems = Get-ChildItem "C:\Users\John Doe\Music\iTunes\iTunes Media\Music\" -Recurse
$newFolder = "C:\Users\Jone Doe\Music\iTunes\iTunes Media\MusicTemp\"
foreach($item in $folderItems)
{
    if( $item.Attributes -eq "Archive")
    {        
       $escapedFullName = $item.FullName.Replace('[', '``[').Replace(']', '``]')
       Copy-Item $escapedFullName $newFolder       
    }    
}

Here is the link that I got the workaround idea from.



Wednesday, August 7, 2013

CentOS - how to change computer name and static IP address (network settings)

To update computer name (hostname) and IP Address in CentOS, edit the following files.

  • Computer Name (host name)
    /etc/sysconfig/network
  • IP Address and other network settings
    /etc/sysconfig/network-scripts/ifcfg-eth0  (or similar file)

Reboot the computer afterwards.