Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell不会逐行迭代MySQL结果-为什么?_Mysql_Loops_Powershell_Line - Fatal编程技术网

PowerShell不会逐行迭代MySQL结果-为什么?

PowerShell不会逐行迭代MySQL结果-为什么?,mysql,loops,powershell,line,Mysql,Loops,Powershell,Line,我使用以下代码使PowerShell打印SQL-SELECT的结果 [String] $mySQlServerName = "localhost"; [String] $mySQLDatenbankName = "test"; [String] $userName = "root"; [String] $password = ""; $mySqlConnection = New-Object MySql.Data.MySqlClient.MySqlConnection; $mySqlConn

我使用以下代码使PowerShell打印SQL-SELECT的结果

[String] $mySQlServerName = "localhost";
[String] $mySQLDatenbankName = "test";
[String] $userName = "root";
[String] $password = "";

$mySqlConnection = New-Object MySql.Data.MySqlClient.MySqlConnection;

$mySqlConnection.ConnectionString = "server=$mySQlServerName;user id=$userName;password=$password;database=$mySQLDatenbankName;pooling=false";

$mySqlConnection.Open();

$mySqlCommand = New-Object MySql.Data.MySqlClient.MySqlCommand;

$mySqlCommand.Connection = $mySqlConnection;

$mySqlCommand.CommandText = "SELECT SourceIPO1, SourceIPO2, SourceIPO3, SourceIPO4, rejections FROM test.malicious_apache;";

write-host $mySqlCommand.CommandText;

$reader = $mySqlCommand.ExecuteReader();

if ($reader -eq $null)
{
    write-host "no result";
}
else
{
    [int] $columnCount = $reader.VisibleFieldCount;
    write-host "$columnCount columns in result ...";

    while ($reader.Read())
    {
        write-output "next row";
        for ($i= 0; $i -lt $reader.VisibleFieldCount; $i++)
        {
            write-output $reader.GetValue($i).ToString()
        }
    }

    $reader.Close();
}

$mySqlConnection.Close();
并获得以下输出

PS C:\Users\Max> TestSelect.ps1
SELECT SourceIPO1, SourceIPO2, SourceIPO3, SourceIPO4, rejections FROM test.malicious_apache;
5 columns in result ...
mysql客户端发布的SELECT提供了大约5966行!作为一名PowerShell新手,我感到惊讶和困惑,这段代码在许多网站上都有使用,对我来说似乎完全合理,为什么我不获得一个单独的输出“下一行”


非常感谢您的帮助

当比较运算符的左操作数为collection时,该运算符的结果不是布尔值,但包含所有元素的数组满足该比较运算符:

$a=1..5
$a -ne 3 # return array with four elements 1,2,4,5
$a -gt 3 # return array with two elements 4,5
$a -eq 3 # return array with one element 3
$a -lt 1 # return empty array
要评估该结果,PowerShell必须枚举集合


DbDataReader
实现
IEnumerable
,因此它被视为PowerShell的集合。要计算
$reader-eq$null
表达式,PowerShell必须枚举
$reader
。作为该枚举的结果,
$reader
将前进到当前结果集的末尾,因此您没有更多的行可读取。要将集合与
$null
进行实际比较,必须颠倒
-eq
运算符的操作数顺序:
$null-eq$reader
$reader-eq$null
->
$null-eq$reader
非常感谢!但是:为什么?超级地狱魔法?