2,187
edits
m (→Formatting: Fix typos) |
(→Connect: Updated with improved ConnectMySQL func) |
||
Line 716: | Line 716: | ||
== MySQL == | == MySQL == | ||
=== Connect === | === Connect === | ||
Make sure you have the .NET connector installed 1st - http://dev.mysql.com/downloads/connector/net/ | |||
<source lang="powershell"> | <source lang="powershell"> | ||
function ConnectMySQL([string]$user,[string]$pass,[string]$MySQLHost,[string]$database) { | function ConnectMySQL([string]$user,[string]$pass,[string]$MySQLHost,[string]$database) { | ||
Line 742: | Line 744: | ||
# Connect to MySQL Database | # Connect to MySQL Database | ||
$conn = ConnectMySQL $user $pass $MySQLHost $database | $conn = ConnectMySQL $user $pass $MySQLHost $database | ||
</source> | |||
Improved connect function with error catcher... | |||
<source lang="powershell"> | |||
function ConnectMySQL([string]$user, [string]$pass, [string]$MySQLHost, [string]$database) { | |||
# Load MySQL .NET Connector Objects | |||
[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data") | |||
# Open Connection | |||
$connStr = "server=" + $MySQLHost + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database+";Pooling=FALSE" | |||
try { | |||
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr) | |||
$conn.Open() | |||
} catch [System.Management.Automation.PSArgumentException] { | |||
Log "Unable to connect to MySQL server, do you have the MySQL connector installed..?" | |||
Log $_ | |||
Exit | |||
} catch { | |||
Log "Unable to connect to MySQL server..." | |||
Log $_.Exception.GetType().FullName | |||
Log $_.Exception.Message | |||
exit | |||
} | |||
Log "Connected to MySQL database $MySQLHost\$database" | |||
return $conn | |||
} | |||
</source> | </source> | ||