I have code which must load MySQL.Data.dll as MySQLConnector and System.Runtime.CompilerServices.Unsafe.dll so that I can use SQL-transactions
[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()
}
The problem is, that I have en eror loading this “unsafe.dll” An error
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."
indicates, that MySQL.Data.dll trying to load some certain version of this ‘unsafe.dll’ and can not do this. And it raises an error.
It there any way for me to fix it somehow? Because I need transactions in my Powershell script, and I can not use SimplySQL module because of some issues.