title: Connection to MySQL from .NET using SSH.NET Library
body: <p><a href="https://i.stack.imgur.com/aNctR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aNctR.jpg" alt="enter image description here"></a></p>
  
  <p>I am developing a web page (ASP.NET/ C#) that queries (MySQL) database on a remote server over SSH. I am using those two libraries (mysql-connector-net-6.9.7) and (Renci.SshNet.dll).</p>
  
  <p>I can access MySQL database using MySQL Workbench on the remote server over SSH connection:<br>
      "<strong>portal.RemoteServer.edu:22</strong>" using "<strong>RemoteServerUsername</strong>" and<br>
      "<strong>RemoteServerPassword</strong>".</p>
  
  <p>Here is my C# code, which doesn't return any data from Clients table on the remote server:</p>
  
  <pre><code>MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();
  connBuilder.AllowBatch = true;
  connBuilder.Server = "127.0.0.1";
  connBuilder.Port = 3306;
  connBuilder.UserID = "LocalHostUserID";
  connBuilder.Password = "LocalHostPassword";
  connBuilder.Database = "DatabaseName";
  
  var auth =
      new PasswordAuthenticationMethod("RemoteServerUsername", "RemoteServerPassword");
  ConnectionInfo conInfo =
      new ConnectionInfo("portal.RemoteServer.edu", "RemoteServerUsername", auth);
  
  using (SshClient client = new SshClient(conInfo))
  {
      ForwardedPortLocal port = new ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 22);
      client.Connect();
      client.AddForwardedPort(port);
      port.Start();
      MySqlConnection conn = new MySqlConnection(connBuilder.ConnectionString);
      conn.Open();
      conn.ChangeDatabase(connBuilder.Database);
  
      var command = "SELECT * FROM DatabaseName.Clients LIMIT 10";
      using (MySqlCommand cmd = new MySqlCommand(command))
      {
          using (MySqlDataAdapter sda = new MySqlDataAdapter())
          {
              cmd.Connection = conn;
              sda.SelectCommand = cmd;
              using (DataTable dt = new DataTable())
              {
                  sda.Fill(dt);
                  GridView1.DataSource = dt;
                  GridView1.DataBind();
              }
          }
      }
  }
  </code></pre>