2014-10-30 36 views
0

我最近的项目需要使用PowerShell查询从MySQL数据库中的数据,我找到一个很好的脚本来做到这一点,唯一的问题是,脚本总是输出int值(精确地返回项目的数量) 。如何避免这种输出?我唯一想获得的是查询数据。 下面是脚本:如何在使用powershell脚本查询数据库时避免int值输出?

$mysqlDllFilePath = "C:\temp\powerShellScript\MySQL.Data.dll" 
[void][system.reflection.Assembly]::LoadFrom($mysqlDllFilePath) 

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection 

$myconnection.ConnectionString = "server=localhost;userid=root;password=admin;database=temp;pooling=false" 

$myconnection.Open() 

$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand 

$mycommand.Connection = $myconnection 

$mycommand.CommandText = "SELECT SiteGuid,SiteName,ProductEdition,ProductVersion from site" 

$adapter = new-object MySql.Data.MySqlClient.MySqlDataAdapter 

$adapter.SelectCommand = $mycommand 

$ds = new-object System.Data.DataSet 

$adapter.Fill($ds) 

$myconnection.close() 
$ds.Tables[0] 

和脚本的输出为:

PS C:\Users\jason> C:\temp\powerShellScript\Get-ConfigSite.ps1 
2 

SiteGuid     SiteName     ProductEdition   ProductVersion 
--------     --------     --------------   -------------- 
123f7267-757c-4864-b0... simulatedSite   PLT      7.1 
526f7267-757c-4864-b0... simulatedSite   PLT      7.1 

如何避免输出值“2”?

+0

的〔请告诉我更好(清洁器)的方式来忽略PowerShell的输出(http://stackoverflow.com/questions/5260125/whats-the-better-cleaner-way-to-ignore-output-in可能重复-powershell) – Matt 2014-10-30 11:58:09

回答

0

好吧,我找到了输出的原因,它是“$ adapter.Fill($ ds)”语句的输出,如果我声明一个变量来保存这个值,它将不会输出到控制台。也许这就是PowerShell的语法。 解决方法是将该语句更改为“$ count = $ adapter.Fill($ ds)”。 无论如何,谢谢。

+0

如果你不想声明一个虚拟变量,把表达式转换为'[void]'eg '[空隙] $ adapter.Fill($ DS)' – 2014-10-30 03:39:50

相关问题