title: SSH connection to MySQL using SSH.NET library
body: <p>I was given a link to use this library for connection through SSH connection to a MySQL database with C#.</p>
  
  <p>Here is the <a href="http://sshnet.codeplex.com/" rel="nofollow">LINK</a> to the library as a lot of you will find this interesting as you don't need to open a SSH channel manually.</p>
  
  <p>This is a very detailed library and has a lot of features but what I want to do is pretty simple. I just want to open a SSH channel each time I want to write to the database.</p>
  
  <p>While I was searching through the source that is provided I came up to this part of the code which can be found in <strong>TestSshCommand.cs</strong> and I think I can use this part for my connections but I need your help to get it work as I have my connection string I don't know how to connect all of it.</p>
  
  <p>Code:</p>
  
  <pre><code>public void Test_Execute_SingleCommand()
          {
              using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
              {
                  client.Connect();
                  var result = ExecuteTestCommand(client);
                  client.Disconnect();
  
                  Assert.IsTrue(result);
              }
  </code></pre>
  
  <p>My code for connectiong to non SSH channel is:</p>
  
  <pre><code>con.ConnectionString = ConfigurationManager.ConnectionStrings["Con2"].ConnectionString;
              con.Open();
              cmd = new MySqlCommand("SELECT COUNT(*) FROM " + ConfigSettings.ReadSetting("main"), con);
  
       //...... more of the code
  </code></pre>
  
  <p>So how can I combine this code to the above one so it can first open a SSH channel and then execute my query?</p>
  
  <p>Do I need to put my code inside <em>connect</em> and <em>disconnect</em> functions?</p>
  
  <p>One more think I have add <strong>.dll</strong> file inside references and I must say that none of the imports are working. So can someone please download the library and try this simple code to see if references are working on your project and so that everyone ( which are a lot ) can have a working solution for their future projects?</p>
  
  <p>EDIT:</p>
  
  <p>This is my code that I was trying to do later on. My SSH connection works now only need to combine those two to get it work.</p>
  
  <p>Problem is that I need to have one database locally on my pc and need to connect to the server. So port forwarding won't work as somone says on the forum that I can't port forward while mysql already have port for running</p>
  
  <pre><code>using (var client = new SshClient("cpanel****", "******", "******"))
            {
              client.Connect();
              con = new MySqlConnection(GetConnectionString());
              con.Open();
              cmd = new MySqlCommand(String.Format("insert into {0} values (null, ?Parname , ?Parname2, ?Parname3, ?Parname4, ?Parname5, ?Parname6, ?Parname7);", ConfigSettings.ReadSetting("main")), con);
              cmd.Parameters.Add("?Parname", MySqlDbType.Double).Value = Math.Round(deciLat, 5);
              // ... more parameters
              cmd.ExecuteNonQuery();
              client.Disconnect();
             }
  </code></pre>