I have used port forwarding to connect to a server running SQL Server 2008 R2 Express. It works great. All I have to do is forward port 1433 to port 1433 on the server and set all the firewall and configuration things right. Now, I want to be able to have a second server running SQL Server Express and be able to route to that server by forwarding port 8081 to an appropriate port on the second server, probably 1433 as well. The port number is not really important here. The problem is the connection string. I need to be able to set the server, userid and password programmatically based on user input. I wrote a small test program to work out the string. If I have the port in the url (e.g., www.rkulp.us:8081
) then the string is invalid. So, can I do this and, if so, how?
Public Class Form1
Dim strConn As String = ""
Dim gMsg As String = ""
Private Sub btnTestConnection_Click(sender As System.Object, e As System.EventArgs) Handles btnTestConnection.Click
strConn = "Data Source="
strConn += Trim(txtServer.Text) + ";Initial Catalog=HouseholdInventory;Persist Security Info=True;User ID="
strConn += Trim(txtLoginName.Text) + ";Password=" + Trim(txtPassword.Text)
Dim TestConn As New SqlClient.SqlConnection
TestConn.ConnectionString = strConn
btnTestConnection.Text = "Successful Connection"
btnTestConnection.BackColor = Color.Green
Catch ex As Exception
gMsg = "The following error occurred trying to build the connection:" + vbCrLf + ex.Message
If Not IsNothing(ex.InnerException) Then gMsg += vbCrLf + "Inner Exception:" + vbCrLf + ex.InnerException.Message
gMsg += vbCrLf + "Connection String: " + vbCrLf + strConn
btnTestConnection.Text = "Connection Failed"
btnTestConnection.BackColor = Color.Red
btnReset.Enabled = True
Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
btnTestConnection.BackColor = Color.LightGray
btnReset.Enabled = False
btnTestConnection.Text = "Test Connection"