How to test an odbc connection with powershell ?

Asked By 20 points N/A Posted on -

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.



Answered By 0 points N/A #131298

How to test an odbc connection with powershell ?



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
    $conn=new-object System.Data.SqlClient.SQLConnection
    $conn.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $ServerInstance,$Database
    $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
    $ds=New-Object system.Data.DataSet
    $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
For non sql based connections.
function Get-OLEDBData ($connectstring, $sql) {
   $OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connectstring)
   $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
   return $dt


Login/Register to Answer

Related Questions