[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
#Creates a connection to the server
$connStr ="server=mySql;Persist Security Info=false;user id=" + "username" + ";pwd=" + "pass" + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
#Open connection
$conn.Open()
#Drops database if it currently exists
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection = $conn
$cmd.CommandText = "DROP DATABASE IF EXISTS " + $dbname
$cmd.ExecuteNonQuery()
#Create the new database
$cmd.CommandText = 'CREATE SCHEMA `' + $dbname + '`'
$cmd.ExecuteNonQuery()
#Load the sql file
$sql = (Get-Content C:\"path to sql file") -join "'r'n"
$cmd.CommandText = $sql
#Close connection
$conn.Close()
答案 0 :(得分:1)
您是否错过了$ cmd.ExecuteNonQuery()调用?看起来你正在用文字“'r'n”加入文件行。您需要使用反向刻度字符(例如 “`r`n”)。我将使用Out-String cmdlet将文件内容转换为一个字符串:
#Load the sql file
$sql = Get-Content C:\"path to sql file" | Out-String
$cmd.CommandText = $sql
$cmd.ExecuteNonQuery()