forked from sagarv26/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractDataFromDB.ps1
29 lines (22 loc) · 1.13 KB
/
ExtractDataFromDB.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Add-Type -Path "C:\Oracle\product\12.1.0\client_1\ODP.NET\managed\common\Oracle.ManagedDataAccess.dll"
$ds=$da=$null
$query = Get-Content C:\query1.txt
$username=" "
$password=" "
$datasource="(DESCRIPTION = (ADDRESS = (PROTOCOL = tcp)(HOST = prd2b-vip.albertsons.com)(PORT = 1551)) (CONNECT_DATA = (SERVICE_NAME = FIN1PRD2) (INSTANCE_NAME = FINPRD2B)))"
$connectionString = 'User Id=' + $username + ';Password=' + $password + ';Data Source=' + $datasource
$connection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)
$connection.open()
Write-Host "Connected.."
$command=$connection.CreateCommand()
$command.CommandText=$query
$reader=$command.ExecuteReader()
$ds = New-Object system.Data.DataSet
$da = New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($command)
#[void]$da.fill($ds)
#return $ds.Tables[0] | Export-CSV "C:\test.csv" -NoTypeInformation
$da.fill($ds) | Out-Null -ErrorAction SilentlyContinue
$data=$ds.Tables[0] | Export-CSV "C:\test.csv" -NoTypeInformation
$connection.Close()
#You will have all the data without being connected stored in the dataset
Write-Host $ds.Tables[0].Rows.Count