How to test an odbc connection with powershell ?

Asked By 20 points N/A Posted on -
qa-featured

Hi Experts,

I am having a odbc connection in my pc.I want to know how to test an odbc connection with powershell.I seek expert advice to get this done.I am having windows xp in my laptop.Thanks.

Regards,

Omarionalejandro

SHARE
Answered By 0 points N/A #131298

How to test an odbc connection with powershell ?

qa-featured

Hi,

Below are the two codes which can be used to test the ODBC connection in your system using powershell for that you need to write the powershell script which can be used for testing the database connection.

For SQL based systems below is the code.

 

function Invoke-Sqlcmd2
{
    param(
    [string]$ServerInstance,
    [string]$Database,
    [string]$Query,
    [Int32]$QueryTimeout=30
    )
 
    $conn=new-object System.Data.SqlClient.SQLConnection
    $conn.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $ServerInstance,$Database
    $conn.Open()
    $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
    $cmd.CommandTimeout=$QueryTimeout
    $ds=New-Object system.Data.DataSet
    $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
    [void]$da.fill($ds)
    $conn.Close()
    $ds.Tables[0]
 
}
 
 
For non sql based connections.
 
function Get-OLEDBData ($connectstring, $sql) {
   $OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connectstring)
   $OLEDBConn.open()
   $readcmd = New-Object system.Data.OleDb.OleDbCommand($sql,$OLEDBConn)
   $readcmd.CommandTimeout = '300'
   $da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)
   $dt = New-Object system.Data.datatable
   [void]$da.fill($dt)
   $OLEDBConn.close()
   return $dt
}

 

Related Questions