William
William

Reputation: 1457

Trouble with Powershell and MySql ExecuteNonQuery

The powershell script I'm working on is trying to run the contents of some script files (.sql) against a database. I know the database connection is working and that the powershell script can query the database because I have been able to have my script confirm that a record exists in the database. The issue I'm having is getting the script to execute an insert against the database. The relevant part of my script is:

function RunNonQuery($commandText) {
    $conn = New-Object MySql.Data.MySqlClient.MySqlConnection
    $conn.ConnectionString = GetConnString
    $conn.Open()
    $cmd = $conn.CreateCommand()
    $cmd.CommandText = $commandText
    $rowsAffected = $cmd.ExecuteNonQuery()
    $cmd.Dispose()
    $conn.Close()
    return $rowsAffected
}

"GetConnString" is just a method that returns the active connection string. The contents of the $commandText variable is:

-- CreationDate=4/11/2013 1:04:51 PM

INSERT INTO Patch (PatchName) VALUES ('TestFromPatchFile');

By debuging the powershell script I can tell that the command text is getting passed into my "RunNonQuery" method correctly. It just doesn't seem to be actually executing the script against the database. $rowsAffected is always 0 and there are no errors when executed. Looking at the actual database I can confirm that the insert is not happening.

Anybody know what's wrong with my "RunNonQuery" method?

Upvotes: 2

Views: 1484

Answers (1)

Frode F.
Frode F.

Reputation: 54841

You should replace the -- comment in your CommandText. MySqlClient might not notice the linebreak, and because of that, it will comment out the whole command.

Upvotes: 1

Related Questions