I try to connect to a remote MySQL server using PowerShell 5.1 on a Windows 10 64 Bit system. I used Powershell x86 and the 64 Bit version.
When I use the Open() method of System.Data.Common.DbConnection I get an error that a file called Renci.SshNet could not be found while calling the Open() method. I wonder why the Renci.SshNet.dll is even needed here, because I don't try to build an SSH connection.
I installed the MySQL dot net connector.
I installed the files for the MySQL module into:
C:\Program Files (x86)\WindowsPowerShell\Modules\MySQL
MySql.psd1
MySQL.psm1
I imported the module MySQL
I imported the MySQL Assembly (whatever this exactly may be):
Result:
GAC Version Location
--- ------- --------
True v4.0.30319 C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\MySql.Data...
I downloaded the Renci.SshNet.dll file and used it like seen in the source code:
[void][Reflection.Assembly]::LoadFrom( (Resolve-Path "$PSScriptRoot\SSH.NET.Fork.2018.8.25.2\lib\netstandard2.0\Renci.SshNet.dll") )
# Import MySQL Assembly
Import-Module MySQL
[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
# Connection string
$strServer = "db.server.test"
$strDB = "mydb"
$strUser = "dbuser"
$strPw = "mypassword"
$cs = "Server=$($strServer);Port=3306;Database=$($strDB);Uid=$($strUser);Pwd=$($strPw);"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($cs)
$conn.ConnectionString = $cs
$conn.Open()
The expected result is getting a connection to the MySQL database, but I always get the error message below:
Ausnahme beim Aufrufen von "Open" mit 0 Argument(en): "Die Datei oder Assembly "Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden."
Exception calling "Open" with 0 arguments: "The file oder Assembly "Renci.SshNet, Version=2016.1.0.0, Culture=neutral, PublicKeyToken=1cee9f8bde3db106" or a dependency could not be found. The system can't find the file.