开发者

Which driver should I install so that mysqlcommand can be run using powershell?

开发者 https://www.devze.com 2023-02-13 19:11 出处:网络
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 i

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 MySq开发者_运维问答l installed in my system and all projects run with MySql very well.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号