Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to mysql from C# over SSH

How can I connect to a mysql database trough C#,

This is my connection string now:

connectionString="server=localhost;port=3306;user id=root;Password=*****;database=Data" providerName="MySql.Data.MySqlClient"

How to put SSH string in this form as it needs to be something like:

SSH Hostname, SSH Username, SSH password, Mysql Hostname, Mysql Username, Mysql Password, Port

like image 561
user123_456 Avatar asked May 29 '12 21:05

user123_456


1 Answers

Here is the final code :) Works for me.

    PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("host", "user", "password");
    connectionInfo.Timeout = TimeSpan.FromSeconds(30);
    var client = new SshClient(connectionInfo);
    client.Connect();
    var x = client.IsConnected;
    ForwardedPortLocal portFwld = new ForwardedPortLocal("127.0.0.1"/*your computer ip*/, "127.0.0.1" /*server ip*/, 3306 /*server mysql port*/);
    client.AddForwardedPort(portFwld);
    portFwld.Start();
    //// using Renci.sshNet 
    var connection = new MySqlConnection("server = " + "127.0.0.1" /*you computer ip*/ + "; Database = DataBaseName; UID = ?; PWD =?; Port = " + portFwld.BoundPort /*very important !!*/);
    connection.Open();
    var k = connection.State;

    connection.Clone();
    client.Disconnect();
like image 87
Seyed Mohammad Hassan Mirdehgh Avatar answered Oct 15 '22 13:10

Seyed Mohammad Hassan Mirdehgh