我有必须将 MySQL.Data.dll 加载为 MySQL\Connector 和 System.Runtime.CompilerServices.Unsafe.dll 的代码,以便我可以使用 SQL 事务
[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()
}
问题是,我在加载这个“unsafe.dll”时遇到错误
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 试图加载此“unsafe.dll”的某个版本,但无法执行此操作。它会引发错误。 我有什么办法可以修复它吗?因为我的 Powershell 脚本中需要事务,并且由于某些问题我无法使用 SimplySQL 模块。
好的,可以了。问题是,您需要准确加载 4.0.4.1 版本的 System.Runtime.CompilerServices.Unsafe.dll,因为通过链接到此特定版本,仅组装(编译)并兼容任何当前的 MySQL.Data.dll。
我刚刚从 NuGet 存储库手动下载了 System.Runtime.CompilerServices.Unsafe.dll 版本 4.0.4.1。我的意思是我没有将它作为包安装,只是通过 NuGet Package Explorer 查看它并双击必要的 *.dll 进行下载。
我的代码从 Powershell 5.0 脚本到 MySQL 服务器的工作事务现在看起来像:
# | Load MySQL Connector from Windows GAC (Global Assembly Cache)
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
# | Set path to dll file
$unsafe_dll_path = "D:\Connectors\from-nuget\net461\System.Runtime.CompilerServices.Unsafe.dll"
# | Load dll for trancactions
[System.Reflection.Assembly]::UnsafeLoadFrom($unsafe_dll_path)
# | define connection
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
# | Create and open connection to MySQL database
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = "server=192.168.0.80;database=mydatabase;user id=sqldbwriter;password=mypassword"
$connection.Open()
# | Begin transaction
$transaction = $connection.BeginTransaction()
try {
# | Create command
$command = New-Object MySql.Data.MySqlClient.MySqlCommand
$command.Connection = $connection
$command.Transaction = $transaction
# | Execute command in transaction's pipe
$command.CommandText = "INSERT INTO mytable (column1, column2) VALUES ('value25', 'value26')"
$command.ExecuteNonQuery()
# | Commit transaction
$transaction.Commit()
Write-Host "Transaction success."
}
catch {
# | Rollback transaction incase of errors
$transaction.Rollback()
Write-Host "Transaction cancelled."
throw
}
finally {
# | Close connection
$connection.Close()
}
此外,正如您所看到的,似乎甚至不需要从他们的网站下载 Oracle 的 MySQL\Connector,因为它已经存储在 Windows GAC(全局程序集缓存)中
GAC Version Location
--- ------- --------
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\MySql.Data\v4.0_8.3.0.0__c5687fc88969c44d\MySql.Data.dll
False v4.0.30319 D:\Connectors\from-nuget\net461\System.Runtime.CompilerServices.Unsafe.dll
1
Transaction success.
附注也许现在也可以使用“LoadFrom”方法而不是“UnsafeLoadFrom”。我还没查过。