【问题标题】:Which driver should I install so that mysqlcommand can be run using powershell?我应该安装哪个驱动程序才能使用 powershell 运行 mysqlcommand?
【发布时间】:2011-02-26 09:16:42
【问题描述】:

我安装了 mysqlconnector [ODBC] 5.1.8 来运行我的 mysqlcommand,但是我收到了这个错误:

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

我应该安装哪个驱动程序 from the mysql connectors site 在 powershell 上运行此命令(或任何 MySql 命令)?

我的系统中安装了最新版本的 MySql,所有项目都使用 MySql 运行良好。

【问题讨论】:

    标签: mysql database powershell connector


    【解决方案1】:

    您应该安装 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
    

    【讨论】:

    • 再次注意:LoadWithPartialName() 已弃用。 Add-Type 是最新版本的 PowerShell 和 .NET 的首选方法。
    【解决方案2】:

    对于运行 Powershell 2.0 并使用 .NET 4 的任何人收到此错误,过程略有不同。您仍然需要 .NET 连接器。

    您需要在 $pshome 目录中创建一个配置文件,以允许 Powershell 与 .NET 4 程序集一起运行。 This answer 提供了一个合适的解决方案,并且 cmets 提供了一些对 64 位机器有用的信息。

    如果您遇到LoadWithPartialName 的问题...原来是deprecated as of Powershell 3.0。此替代方案适用于 2 和 3,并且由于它是一个 cmdlet,因此可能更容易排除故障:

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

    其中 $path 是MySql.Data.dll 所在的目录。

    【讨论】:

      【解决方案3】:

      把这个文件MySql.Data.dll 在 powershell.exe 所在的目录中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-12
        • 1970-01-01
        相关资源
        最近更新 更多