Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: SSH Tunnel / Port Forwarding

$
0
0
where you able to get it to connect, because i'm having the same problem using C#, trying to connect to mysql database after connecting to the server but no success

by the way i have the same connection setup in the MYSQL workbench and its working fine

for the below code the server is using the host "data" not "localhost", don't know why, but that's how they set it up.

the error i get is "Unable to connect to any of the specified MySQL hosts."
 Console.WriteLine("Ssh connection sucessful");
                            uint sqlPort = 3307;
                            var portFwd = new ForwardedPortLocal("127.0.0.1", sqlPort, "data", sqlPort);
                            client.AddForwardedPort(portFwd);
                            portFwd.Start();
                            if (portFwd.IsStarted)
                            {
                                Console.WriteLine("Port Forwarding has started.");
                            }
                            else
                            {
                                Console.WriteLine("Port Forwarding has failed.");
                            }

                            string connStr = "server=data;database=db_name;uid=my_sql_username;pwd=my_sql_server_password;"; 
                                                        
                            MySqlConnection mysqlConn = new MySqlConnection(connStr); 

                            try
                            {
                                Console.WriteLine("Connecting to MySQL...");
                                mysqlConn.Open();
                                Console.WriteLine("MySql database connection activated");
                                string query = "select * from table_t1";  
                                MySqlCommand cmd = new MySqlCommand(query, mysqlConn);
                                cmd.ExecuteNonQuery();
                                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                                DataTable data = new DataTable();

                                da.Fill(data);

                                dataGridView1.DataSource = data;

                                Console.WriteLine("Query Successful");

                            }
                            catch (SocketException ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                            finally
                            {
                                mysqlConn.Close();
                            }

                        }
                        else
                        {
                            Console.WriteLine("Ssh connection failed");
                        }
any help would be great

thanks alot

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>