Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

The application that I am currently writing needs to be able to query a database located on a remote SQL server. My application can query a database located on a SQL server on the same domain/network, but when I connect via a VPN to a remote server with the same database, the connection fails! Would someone please help guide me in the right direction?

Below is the SQL Connection portion of my code:

C++
private void buttonExecute_Click(object sender, EventArgs e)
        {
            dataGridViewResults.DataSource = null;

            // Create a SQL connection instance to the NovaPACS Database
            string dbString = "Network Library=DBMSSOCN;user id=" + Connection.currentUserName + ";password=" + Connection.currentPassword +
                ";Data Source=" + Connection.currentIP + ";Integrated Security=SSPI;Initial Catalog=" + Connection.currentDbName + ";Trusted_Connection=yes;" +
                      "connection timeout=10";

            SqlConnection SQLConnect = new SqlConnection(dbString);

            MessageBox.Show(dbString);

            try
            {
                // Inject the SQL Query From The Console
                SqlCommand fromQConsole = new SqlCommand(richTextBoxQConsole.Text, SQLConnect);

                DataSet ds = new DataSet();

                SQLConnect.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(fromQConsole);
                adapter.Fill(ds);

                try
                {
                    SQLConnect.Close();
                }
                catch
                {
                    MessageBox.Show("Error: Database Connection Terminated Abnormally!",
                       "ScriptBox", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                // Display Query Results
                dataGridViewResults.DataSource = ds.Tables[0];

            }
 
           catch
            {
                MessageBox.Show("Error: Cannot Open Database Connection! Check Your Connection: Please Ensure That You Have" +
                    "Established A VPN Connection (If Applicable) Or Have Entered The Proper Authentication Credentials!",
                    "ScriptBox", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                SQLConnect.Close();
            }
            catch
            {
                MessageBox.Show("Error: Database Connection Terminated Abnormally!",
                   "ScriptBox", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
Posted
Updated 29-Jan-11 4:01am
v3
Comments
jim lahey 29-Jan-11 10:31am    
please post more information from the exception you get
Shahriar Iqbal Chowdhury/Galib 29-Jan-11 10:38am    
increase the connection timeout

1 solution

If you had provided the error message, it would have been easier.

I would recommend to debug the code and see what error you are getting.

Anyways, here are few things to check:

1. Does the server allows remote connection? (SQL Server Surface Area Configuration can be used to check and set up)
2. If it does, does it allows to connect via TCP/IP? (Since you have mentioned that NetworkLibrary in connection string)
3. Get rid of TrustedConnection part since you have provided the user id and password.
4. IP address should be accompanied by a port number in case you have changed it from default one.


Few other things to be considered:

1. Try/Catch setup in that code does not looks effective.
2. You are trying to close the connection twice.
3. Catch blocks are generic. It should be made specific rather than assuming code failed while opening the connection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900