我应该安装哪个驱动程序以便 mysqlcommand 可以使用 powershell 运行?

5 mysql database powershell connector

我安装了 mysqlconnector [ODBC] 5.1.8 来运行我的 mysqlcommand,但出现此错误:

Cannot find type [MySql.Data.MySqlClient.MySqlConnection]: make sure the assembly containing this type is loaded
Run Code Online (Sandbox Code Playgroud)

我应该从 mysql 连接器站点安装哪个驱动程序才能在 powershell 上运行此命令(或任何 MySql 命令)?

我的系统中安装了最新版本的 MySql,所有项目都可以很好地使用 MySql。

nos*_*nos 3

您应该安装 Connector/Net ,它会自行安装在 GAC 中,并且像任何其他 .Net 程序集一样可用。

然后你可以做例如

[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
Run Code Online (Sandbox Code Playgroud)

  • 再次注意:“LoadWithPartialName()”已被弃用。“Add-Type”是最新版本的 PowerShell 和 .NET 的首选方法。 (2认同)