You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本中MySQL事务依赖的Unsafe.dll版本不匹配问题如何解决?

解决MySQL.Data.dll依赖System.Runtime.CompilerServices.Unsafe版本问题

问题详情

执行的PowerShell脚本

[System.Reflection.Assembly]::LoadFrom("D:\MySQL.Data.dll")
[System.Reflection.Assembly]::LoadFrom("D:\System.Runtime.CompilerServices.Unsafe.dll")

$server = "192.168.0.80"
$database = "mydb"
$user = "sql"
$password = "mypass1"

$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = "server=$server;database=$database;uid=$user;pwd=$password"

$connection.Open()

$transaction = $connection.BeginTransaction()

try {

    $cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
    $cmd.Connection = $connection
    $cmd.Transaction = $transaction


    $cmd.CommandText = "INSERT INTO mytable (column1, column2) VALUES (@value1, @value2)"
    $cmd.Parameters.AddWithValue("@value1", "somevalue1") | Out-Null
    $cmd.Parameters.AddWithValue("@value2", "somevalue2") | Out-Null
    $cmd.ExecuteNonQuery() | Out-Null


    $transaction.Commit()

}
catch {

    if ($transaction -ne $null) {
        $transaction.Rollback()
    }
    Write-Host "Error: $_"
}
finally {

    $connection.Close()
}

报错信息

Exception when calling "BeginTransaction" with "0" arguments: "Could not load file or assembly "System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" or one of its dependencies. The specified file could not be found."

问题说明

MySQL.Data.dll要求加载特定版本4.0.4.1的System.Runtime.CompilerServices.Unsafe.dll,当前加载的dll版本不匹配或路径错误,导致事务功能无法正常执行,且无法使用SimplySQL模块。


解决方法

1. 下载匹配版本的依赖dll

  • 找到版本为4.0.4.1的System.Runtime.CompilerServices.Unsafe.dll,将其放到D:\目录下(和MySQL.Data.dll同路径),确保脚本加载路径正确。
  • 注意:要选择对应架构的dll(x86/x64),和运行PowerShell的架构一致。

2. 配置程序集绑定重定向

在PowerShell脚本开头添加以下代码,强制让程序加载已有的兼容版本代替要求的4.0.4.1版本:

# 注册程序集绑定重定向
$bindingRedirect = @"
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="你已有的版本号" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>
"@

# 将绑定重定向写入临时配置文件
$configPath = Join-Path $env:TEMP "powershell.exe.config"
Set-Content -Path $configPath -Value $bindingRedirect -Encoding utf8

# 加载配置
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
  • 把你已有的版本号替换成你当前已加载的System.Runtime.CompilerServices.Unsafe.dll的实际版本(比如查看dll属性里的版本信息)。

3. 通过NuGet获取完整依赖包

  • 打开命令行,执行以下命令安装MySQL.Data包(需先安装NuGet命令行工具):
    nuget install MySQL.Data -Version 对应版本
    
  • 安装完成后,从packages\MySQL.Data.x.x.x\lib\netxxxx目录下找到MySQL.Data.dll,同时从packages\System.Runtime.CompilerServices.Unsafe.x.x.x\lib\netxxxx目录下找到匹配版本的dll,将这两个dll放到同一目录后再加载。

内容的提问来源于stack exchange,提问作者alive-one

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 10:12:22