Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

.net - Powershell MySQL Delete return exception

Powershell MySQL Delete return exception

Hi,

I have a script written in Powershell that delete row from a table every night (base on a scheduled task). I always received an exception saying :

Write-MySQLQuery : DELETE FROM AZS_Orphaned_NIC WHERE Present ='0' At C:script.ps1:363 char:1 Write-MySQLQuery $DBconnect $delNIC CategoryInfo : NotSpecified: (:) [Write-Error], Exception FullyQualifiedErrorId : System.Exception,Write-MySQLQuery Write-MySQLQuery : DELETE FROM table WHERE Present ='0'

I don't understand what wrong with the query. How can I have more information about the error ?

Here is the code :

function Connect-MySQL([string]$user, [string]$pass, [string]$MySQLHost, [int]$MySQLPort, [string]$MYSQLDatabase) {
    [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
    $connStr = "server=" + $MySQLHost + ";port=" + $MySQLPort + ";uid=" + $user + ";pwd=" + $pass + ";database=" + $MYSQLDatabase + ";Pooling=FALSE;Allow Zero Datetime=true;Connect Timeout=60"
    $conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
    $conn.Open()
    $cmd = New-Object MySql.Data.MySqlClient.MySqlCommand("USE $database", $conn)
    return $conn
 
}

function Write-MySQLQuery($conn, [string]$query) {  
    Try { 
        $command = $conn.CreateCommand()
        $command.CommandText = $query
        $RowsInserted = $command.ExecuteNonQuery()
        $command.Dispose()
        if ($RowsInserted) {
            return $true
        }
        else {
            Write-Error -Exception $query
            return $false
        }
    }
    Catch {
        Write-Error -Exception $_.Exception
        return $false
    }
}

$delNIC = "DELETE FROM table WHERE Present ='0'"
Write-MySQLQuery $DBconnect $delNIC 

Thanks for your help

question from:https://stackoverflow.com/questions/65599248/powershell-mysql-delete-return-exception

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...