我的PowerShell腳本的偉大工程,返回1 DataTable
來自不同數據庫的兩行,但我不能工作了如何將這些2行進行比較。比較結果從2個數據庫服務器下面
的情況是我有2臺數據庫服務器,並希望從同一個表上都比較max(id)
,比較,然後如果他們是不同的可能警報。
警報位,我可以做,但我以前沒有使用過DataTable
秒。
function readServer1 {
# Connection variables
$server = "db1"
$port = 1234
$driver = "Adaptive Server Enterprise"
$query = "select max(id) as 'id' from table"
$db = "db"
$uid = "uid"
$pwd = "pwd"
# Create Object and Connection
$conn = New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString = "driver={$driver};db=$db;na=$server,$port;uid=$uid;pwd=$pwd;"
$conn.Open()
$cmd = New-Object System.Data.Odbc.OdbcCommand($query, $conn)
$cmd.CommandTimeout = 30
# Create a Data Table
$dt = New-Object System.Data.DataTable
$dt.Load($cmd.ExecuteReader())
$dt.Rows
# Close Connection
$conn.Close()
}
function readServer2 {
# Connection variables
$server = "db2"
$port = 1234
$driver = "Adaptive Server Enterprise"
$query = "select max(id) as 'id' from table"
$db = "db"
$uid = "uid"
$pwd = "pwd"
# Create Object and Connection
$conn = New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString = "driver={$driver};db=$db;na=$server,$port;uid=$uid;pwd=$pwd;"
$conn.Open()
$cmd = New-Object System.Data.Odbc.OdbcCommand($query, $conn)
$cmd.CommandTimeout = 30
# Create a Data Table
$dt = New-Object System.Data.DataTable
$dt.Load($cmd.ExecuteReader())
$dt.Rows
# Close Connection
$conn.Close()
}
readServer1
readServer2
它返回的結果是這樣的:
id
--
12345
12346
你想通過比較來實現什麼?如果你只是想知道,如果兩個結果不同,你可以這樣做:'如果($比較(readserver1)$(readserver2)){//不同}' – Paul
我覺得我跑的方式進入1個表沒有似乎有助於比較方法。 – benji1304