This project is read-only.

Connect to ms sql server using winforms

Jan 14, 2015 at 9:31 AM
Hello,

I'm having trouble connecting to a remote MS sql server (2008) through the ssh.net library.
I can create the ssh tunnel to the sql server, and test ok but when I try to connect to the db from my winform application I get this error:
"A connection was successfully established with the server, but then an error occurred during the pre-login handshake"

However if i use putty to establish the ssh tunnel it works fine!?

Any thoughts and help would be greatly appretiated since this is the last piece in my puzzle :)

Best Regards

Janne

Here's my code:
        PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("ssh.mydomain.se", "username", "password");
        connectionInfo.Timeout = TimeSpan.FromSeconds(30);
        var SSHclient = new SshClient(connectionInfo);
        var portFwld = new ForwardedPortLocal("127.0.0.1", Convert.ToUInt32(5103), "db_name.mssql.mydomain.se", Convert.ToUInt32(1433));

        using (SSHclient)
        {
            try
            {
                SSHclient.Connect();

                if (SSHclient.IsConnected)
                    label1.Text = "Conn success!!!";
                else
                    label1.Text = "Conn fail!!!";

                SSHclient.AddForwardedPort(portFwld);
                portFwld.Start();

                if (SSHclient.IsConnected)
                    label2.Text = "portFwld success!!! BoundHost = " + portFwld.BoundHost.ToString() + " Port = " + portFwld.BoundPort.ToString() + " IsStarted = " + portFwld.IsStarted.ToString();
                else
                    label2.Text = "portFwld fail!!!";

            }
            catch (SshException b)
            {
                label1.Text = "SSH client connection error: {0}" + b.Message;
            }
            catch (System.Net.Sockets.SocketException a)
            {
                label2.Text = "Socket connection error: {0}" + a.Message;
            }
        }
//Connect do DB
        string strConnection = "data source=127.0.0.1,5103;user id = myusername; password = mypassword; MultipleActiveResultSets=True";
        SqlConnection sql = new SqlConnection(strConnection);
        try
        {
            sql.Open();
            label3.Text = "SUCCESS!!!";
        }
        catch (SqlException c)
        {
            label3.Text = "Error. Cause: " + c.Message;
        }
        finally
        {
            sql.Close();
        }