如何使用powershell将SQL数据获取到WPF数据网格中?
我正在尝试将sql查询数据添加到WPF powershell应用程序中。在如何使用powershell将SQL数据获取到WPF数据网格中?,wpf,powershell,datagrid,Wpf,Powershell,Datagrid,我正在尝试将sql查询数据添加到WPF powershell应用程序中。在$dg项中,我看到了SQL查询返回的计算机名,但m datagrid没有填充 这是我的密码 function Invoke-sql1 { param( [string]$sql, [System.Data.SQLClient.SQLConnection]$connection ) $cmd = new-object System.Data.SQLClient.SQLCommand(
$dg
项中,我看到了SQL查询返回的计算机名,但m datagrid没有填充
这是我的密码
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0].rows
}
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
AutoGenerateColumns="False"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
Width="330"
HeadersVisibility="All"
>
<DataGrid.Columns>
<DataGridTextColumn Header="SystemName"
Binding="{Binding SystemName}"
/>
<DataGridTextColumn Header="ProcessorLoad"
Binding="{Binding ProcessorLoad}"
/>
<DataGridTextColumn Header="CreatedDate"
Binding="{Binding CreatedDate}"
/>
</DataGrid.Columns>
</DataGrid >
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString=@"
DataSource=JAPDC\SQLEXPRESS;
IntegratedSecurity=True;
Initial Catalog=TestDB;
"@
$con.open()
$sql = “SELECT t1.SystemName, t1.ProcessorLoad, t1.CreatedDate
FROM [TestDB].[dbo].[CPUPerformance] t1
INNER JOIN (SELECT Max(CreatedDate) CreatedDate, SystemName
FROM [TestDB].[dbo].[CPUPerformance]
GROUP BY [TestDB].[dbo].[CPUPerformance].SystemName ) AS t2
ON
t1.SystemName = t2.SystemName
and t1.CreatedDate = t2.CreatedDate
ORDER BY CreatedDate DESC ”
$dg = $Form.FindName("dataGrid1")
$dg.ItemsSource = @(Invoke-sql1 $sql $con)
$dg
$Form.ShowDialog()
函数调用-sql1
{
参数([string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd=new object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds=新对象system.Data.DataSet
$da=新对象System.Data.SQLClient.SQLDataAdapter($cmd)
$da.填写($ds)|填写为空
返回$ds.tables[0]。行
}
[xml]$xaml=@”
"@
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$Form=[Windows.Markup.XamlReader]::加载($reader)
$con=新对象System.Data.SqlClient.SqlConnection
$con.ConnectionString=@“
DataSource=JAPDC\SQLEXPRESS;
IntegratedSecurity=True;
初始目录=TestDB;
"@
$con.open()
$sql=”选择t1.SystemName、t1.ProcessorLoad、t1.CreatedDate
来自[TestDB].[dbo].[CPUPerformance]t1
内部联接(选择Max(CreatedDate)CreatedDate,SystemName
来自[TestDB].[dbo].[CPUPerformance]
将[TestDB].[dbo].[CPUPerformance].SystemName]分组为t2
在…上
t1.SystemName=t2.SystemName
和t1.CreatedDate=t2.CreatedDate
按CreatedDate DESC排序”
$dg=$Form.FindName(“dataGrid1”)
$dg.ItemsSource=@(Invoke-sql1$sql$con)
$dg
$Form.ShowDialog()
如果有其他方法,请告诉我。在函数Invoke-sql1中,将return语句更改为: 返回$ds.tables[0].DefaultView 或者,如果希望继续返回.rows,则需要更改要索引到ItemArray的绑定
Binding="{Binding ItemArray[0]}"
Binding="{Binding ItemArray[1]}"
etc...