Question

I installed the mysqlconnector [ODBC] 5.1.8 for running my mysqlcommand, but I got this error:

Cannot find type [MySql.Data.MySqlClient.MySqlConnection]: make sure the assembly containing this type is loaded

Which driver should I install from the mysql connectors site to run this command (or any MySql command) on powershell?

I have the latest version of MySql installed in my system and all projects run with MySql very well.

Was it helpful?

Solution

You should install the Connector/Net , it installs itself in the GAC, and is available like any other .Net assembly .

Then you can do e.g.

[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$connectionString = "server=myserver;uid=myuser;pwd=mypass;database=mydb;"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$sql = "show tables"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
$table = New-Object System.Data.DataTable
$recordCount = $dataAdapter.Fill($table)
echo $table
echo $table | ogv

OTHER TIPS

For anyone receiving this error that is running Powershell 2.0 and using .NET 4, the procedure is slightly different. You will still need the .NET connector.

You will need to create a config file in the $pshome directory to allow Powershell to run with .NET 4 assemblies. This answer provides an appropriate solution to do so, and the comments have some helpful information for 64-bit machines.

If you are experiencing trouble with LoadWithPartialName... turns out it is deprecated as of Powershell 3.0. This alternative will work in both 2 and 3, and may be a bit easier to troubleshoot since it is a cmdlet:

Add-Type -Path '$path\MySql.Data.dll'

Where $path is the directory that MySql.Data.dll is located.

put this file MySql.Data.dll in directory where powershell.exe is residing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top