2

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?

2
  • 1
    Does it help if you remove the comments? It might not pick up on the linebreak, and run the whole text as a comment. Commented Apr 11, 2013 at 22:12
  • Yeah that was it. I updated my script to use the /* */ comment style and it works fine. If you want to create an answer I'll accept it. Commented Apr 11, 2013 at 22:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.