Im writing a script in powershell that needs to connect to the MySQL server, create a new database and then run query commands with this database. I have a ready .sql file with all the commands that will be executed, but there seems to be a problem with my code where it never executes the .sql file. Any help would be greatly appreciated. My code is below.
[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
$connStr ="server=mySql;Persist Security Info=false;user id=" + "username" + ";pwd=" + "pass" + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
$conn.Open()
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection = $conn
$cmd.CommandText = "DROP DATABASE IF EXISTS " + $dbname
$cmd.ExecuteNonQuery()
$cmd.CommandText = 'CREATE SCHEMA `' + $dbname + '`'
$cmd.ExecuteNonQuery()
$sql = (Get-Content C:\"path to sql file") -join "'r'n"
$cmd.CommandText = $sql
$conn.Close()
G3th source
share