C# connect WinForm App running locally via Remote Desktop (or other) to a Database

Viewed 1903

EDIT

RDP is just the current way I do this, if there is another way to connect to a database in a different environment I will do that. Ideally I want this to connect to the database as it would if the executable was in the RDP environment and show no indication of connecting to the DB via the other environment.

EDIT 2

I have also tried adding a second connection string and opening it then having my current connection string immediately after, that didn't work.


I have a C# Win Forms application which connects to a database, this works fine except I need to RDP into the correct environment and have the executable ran from there to do so.

Is it possible to do the RDP connection inside of the application then connect to the database without having to have the executable inside the RDP.

I have tried to build up a connection string using the MSTSCLib library which I found here however this isn't working at code level anyway.

Code:

    private void rdpConnect_Click(object sender, EventArgs e)
    {
        MSTerminalServiceControl1.Server = rdpServer.Text;
        MSTerminalServiceControl1.UserName = rdpUserName.Text;
        IMsTscNonScriptable secured = (IMsTscNonScriptable)MSTerminalServiceControl1.GetOcx();
        secured.ClearTextPassword = rdpPassword.Text;
        MSTerminalServiceControl1.Connect();
    }

    private void rdpDisconnect_Click(object sender, EventArgs e)
    {
        MSTerminalServiceControl1.Disconnect();
    }

Current Path:

enter image description here

Desired Path:

enter image description here

5 Answers

Looks like you do not have references to the ActiveX Control.

In your Toolbox Choose the COM Component : Microsoft RDP Control - version x

The version here is important it should match the installed RDP version, once you have added this in your tool box drop it on your form, if there is version mismatch it would throw error. I would say start from the latest version of control and go down till you are able to successfully add it to your form. The MSTerminalServiceControl1 in your code is actually the name of this control that you should thus put on your form.

I'm sure, that can not work, because...

RDP (Remote Desktop Protocol) is a Protocol to connect you from a remote machine to a session on a Terminal Server. That's the purpose. In that session, you can start programs, open documents and do your work. Like you were sitting in front of that machine. The benefit is, this can be done by multiple users, depending only on the number of user licenses and hardware power.

There a some special cases, e.g. you can start a program automatically on start up. Then your session is limited to this program and will end, when the program ends. But this is only a restricting. You will still start a session for that.

Also, there is a small version of Terminal Server build in every windows version, not only servers. This can be used a for a remote connection in the same way.

So, sad that, RDP can not connect to a database. In no way. You need a program for that. You can build a local program with a RDP-Client inside. That can connect to a session on a remote machine and start another program, that connects to the database. But you will have no communication between the to programs by default, so there is no benefit.

I think, like others said, the best is to connect to the database directly.

What you need is to Connect multiple sql users to one database and no to use RDP.

you should write a special connection string, for each user something like below:

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
    "Data Source=ServerName;" +
    "Initial Catalog=DataBaseName;" +
    "User id=" + UserName + ";"
    "Password=" + Password + ";";
 conn.Open();

Put this in a class that accepts the username and password.

Full example:

class myConnection
{
    public static SqlConnection GetConnection(string UserName, string Password)
    {
        string str = "Data Source=ServerName;Initial Catalog=DataBaseName;User id=" +             
        UserName + ";Password=" + Password + ";";
        SQlConnection con = new SqlConnection(str);
        con.Open();
        return con;
    }
}

Then connect each user to DB directly.

Provided you have app.config file for the executable, update the connection string parameter to point to the ip of rdp server. Before you do this enable database port on firewall and also enable remote connections to database. For mssql server. Run sql server configuration manager. Select database instance. Enable tcp/ip under network configuration. Under io addresses tab. Set tcp port to 1443. Restart instance.

Data Source=IP,1443\dbservername; I hop this can be a starting point.

I've seen a Network Administrator configured our normal winform application to be opened remotely over the Internet using Citrix. We made no changes to the exectuable file. It will launch the application in the Citrix Virtual Server.

The problem is that it's a paid software.

Related