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
How to test an odbc connection with powershell ?
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
}
Â