Powershell SQL 查询 - 属性 "Site" 有问题
Powershell SQL Query - Problem with a property "Site"
我是 powershell 和 SQL 查询的初学者。我想出了如何进行查询并得到了一些结果,但我 运行 遇到了问题并且不知道如何处理它。
我的代码:
$dataSource = "MyBdServer"
$database = "DabaseName"
$connectionString = "Data Source=$dataSource; " + "Integrated Security=SSPI; " + "Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$sqlCommand = "select Site, Pavillon, Floor, Localisation, Description from [DabaseName].dbo.Local_C where Location_ID In ( '6096B3F168C546BE84A7A98C8210E947')"
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
输出
Site : NCH
Pavillon : D
Floor : Level S3
Localisation : D.S3.5113
Description : CONSULT./ENTREVUE
问题:当我尝试通过站点属性($DataSet.Tables.Site)获取值时,值始终为空,它适用于 Pavillon、Floor、Localization 和 Description。我认为这是因为默认情况下对象 System.Data.Dataset 有一个具有该名称的 属性。我正在尝试找到一种使用此值的方法。
DataColumn
has a Site
property which is why you're unable to reference the values of the Site
column. There are two easy alternatives, the easiest one is to reference the .Table
property of your DataTable
and then the .Site
property and the other alternative is to use the .ToTable(..)
method from DataView
.
$columns = @(
'Site'
'Pavillon'
'Floor'
'Localisation'
'Description'
)
$dtt = [System.Data.DataTable]::new()
$columns | ForEach-Object{
$dtt.Columns.Add([System.Data.DataColumn]::new($_))
}
$row = $dtt.NewRow()
$row.Site = 'NCH'
$row.Pavillon = 'D'
$row.Floor = 'Level S3'
$row.Localisation = 'D.S3.5113'
$row.Description = 'CONSULT./ENTREVUE'
$dtt.Rows.Add($row)
$dtt.Site # => Doesn't work
$dtt.Table.Site # => Works
$dv = [System.Data.DataView]::new($dtt)
$dv.ToTable($true, 'Site') # => Works
我是 powershell 和 SQL 查询的初学者。我想出了如何进行查询并得到了一些结果,但我 运行 遇到了问题并且不知道如何处理它。
我的代码:
$dataSource = "MyBdServer"
$database = "DabaseName"
$connectionString = "Data Source=$dataSource; " + "Integrated Security=SSPI; " + "Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$sqlCommand = "select Site, Pavillon, Floor, Localisation, Description from [DabaseName].dbo.Local_C where Location_ID In ( '6096B3F168C546BE84A7A98C8210E947')"
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
输出
Site : NCH
Pavillon : D
Floor : Level S3
Localisation : D.S3.5113
Description : CONSULT./ENTREVUE
问题:当我尝试通过站点属性($DataSet.Tables.Site)获取值时,值始终为空,它适用于 Pavillon、Floor、Localization 和 Description。我认为这是因为默认情况下对象 System.Data.Dataset 有一个具有该名称的 属性。我正在尝试找到一种使用此值的方法。
DataColumn
has a Site
property which is why you're unable to reference the values of the Site
column. There are two easy alternatives, the easiest one is to reference the .Table
property of your DataTable
and then the .Site
property and the other alternative is to use the .ToTable(..)
method from DataView
.
$columns = @(
'Site'
'Pavillon'
'Floor'
'Localisation'
'Description'
)
$dtt = [System.Data.DataTable]::new()
$columns | ForEach-Object{
$dtt.Columns.Add([System.Data.DataColumn]::new($_))
}
$row = $dtt.NewRow()
$row.Site = 'NCH'
$row.Pavillon = 'D'
$row.Floor = 'Level S3'
$row.Localisation = 'D.S3.5113'
$row.Description = 'CONSULT./ENTREVUE'
$dtt.Rows.Add($row)
$dtt.Site # => Doesn't work
$dtt.Table.Site # => Works
$dv = [System.Data.DataView]::new($dtt)
$dv.ToTable($true, 'Site') # => Works