Difference between revisions of "MySQL and PowerShell"

Jump to navigation Jump to search
251 bytes added ,  21:00, 14 August 2017
→‎Connect: Made function names consistent
m (→‎Reader Query: Corrected error in example)
(→‎Connect: Made function names consistent)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
To be able to interact with a MySQL database from PowerShell you need to have the MySQL .NET Connector installed first - http://dev.mysql.com/downloads/connector/net/
{{#widget:Widget:GoogleAdSenseSkyscraper}}
To be able to interact with a MySQL database from PowerShell you need to have the MySQL .NET Connector installed first on the machine that you're going to run PowerShell from - http://dev.mysql.com/downloads/connector/net/


= Connect =
= Connect =
Line 19: Line 20:
   $conn.Close()
   $conn.Close()
}
}
</source>


# So, for example...
So, for example...
 
<source lang="powershell">
# Connection Variables  
# Connection Variables  
$user = 'myuser'  
$user = 'myuser'  
Line 34: Line 36:
Improved connect function with error catcher...
Improved connect function with error catcher...
<source lang="powershell">
<source lang="powershell">
function ConnectMySQL([string]$user, [string]$pass, [string]$MySQLHost, [string]$database) {  
function Connect-MySQL([string]$user, [string]$pass, [string]$MySQLHost, [string]$database) {  
     # Load MySQL .NET Connector Objects  
     # Load MySQL .NET Connector Objects  
     [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")  
     [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")  
Line 58: Line 60:
}  
}  
</source>
</source>
{{#widget:Widget:GoogleAdSenseSkyscraper}}


= Commands =
= Commands =
Line 145: Line 148:


= Test for NULL =
= Test for NULL =
In order to be able to test for a NULL value in a set of results from a query, you need to compare against a specific DB NULL value.  Otherwise you'll get an error similar to...
In order to be able to test for a NULL value in a set of results from a query, you need to compare against a specific DB NULL value.  Otherwise you can get errors similar to...


<code> Error: "Cannot convert the "0" value of type "System.Int32" to type "System.DBNull" </code>
Error: "Cannot convert the "0" value of type "System.Int32" to type "System.DBNull"  
Method invocation failed because [System.DBNull] doesn't contain a method named 'xxx'.


So to correctly test, use the following...
So to correctly test, use the following...

Navigation menu