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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With