我正在尝试在 powershell 中使用 DataTables,更具体地说,我正在尝试检查数据表中是否存在具有特定值的行。
这是我用来创建数据表的方法。
function GetPropertyIDs()
{
write-host "Retrieving all Property ID's from $DatabaseInstance.$DatabaseName..." -NoNewline
$cn = new-object system.data.SqlClient.SqlConnection("Data Source=$DatabaseInstance;Integrated Security=SSPI;Initial Catalog=$DatabaseName");
$cn.Open()
$cmd = new-Object System.Data.SqlClient.SqlCommand("Select Distinct CAST(ID as varchar(5)) as ID, Name, Code from TableName Order by ID", $cn)
$cmd.CommandType = [System.Data.CommandType]'Text'
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $cmd
$dataTable = New-Object System.Data.DataTable
$adapter.Fill($dataTable) | out-null
$cn.Close() | out-Null
$cn.Dispose()
$count = $dataTable.rows.count;
write-host "$count records retrieved."
#Set Primary Key
$pk = $dataTable.Columns["ID"]
$dataTable.PrimaryKey = $pk
return $dataTable
}
这是我试图检查数据表中是否存在特定值的代码...
$PropertyIDs = GetPropertyIDs
foreach($PropertySite in $PropertySites)
{
$id = ([System.String] $PropertySite).Replace("$SiteCollectionURL/property/", "")
$found = $PropertyIDs.DefaultView.Find($id)
}
目前我收到一个错误:
“您不能在空值表达式上调用方法。”
我也试过这个,但也没有运气:
$found = $PropertyIDs.Rows.Contains($id)