0

One of my applications doesn't connect to the DB server anymore. This application has been in production for 2 years, without a change and has always worked.

I'm using C# 2010, and MS SQL Server 2008 R2

  • I get the error:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

  • Inner Exception: "Access is denied"

  • Using the exact same code, connection string, Database & DB Server, I am able to connect with another application, without any errors.

  • I am able to connect using SSMS on the server and databases.

  • The query (if copied to SSMS) runs without a problem.

CODE:

public DataTable QueryRetrieval(string sqlQuery)
    {
        // SP Command
        SqlConnection Conn = new SqlConnection(this.connectionString);
        this.Cmd = new SqlCommand(sqlQuery, Conn);
        Cmd.CommandType = CommandType.Text; // Type of command

        // Declarations (for scope purposes)
        SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);

        DataTable DtResultSet = new DataTable();

        try
        {
            // Run the query
            Conn.Open();
            Adapter.Fill(DtResultSet);

            Conn.Close();

            // Return datatable
            return DtResultSet;
        }
        catch (Exception Ex)
        {
            // Edit the message, rethrow exception
            throw new Exception(
                "DataAccessor.QueryDataTable: Failed to run query '" + sqlQuery + "'\n\n"
                + "Error message: \n--------------\n\n" + Ex.Message);
        }
        finally
        {
            // Remove the SQL command
            Cmd.Dispose();
            Cmd = null;

            // Remove the SQL data adapter
            Adapter.Dispose();
            Adapter = null;
        }

    }

Connection string: "Server=MyServer;Database=MyDB;Trusted_Connection=True;" (I substituted the names.)

The program fails on Conn.Open(); When the codes hits that line, I have to wait 20 or 30 seconds and then the exception is thrown.

No updates have been installed on the server. I have already tried to reboot the server (entire server, not the service).

Is there anyone who has a clue what could cause this to happen?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jan Solo
  • 183
  • 1
  • 8
  • 19
  • Are you able to ping the DDBB server from the machine hosting the app? – Oscar Sep 07 '16 at 10:09
  • 1
    Possible duplicate of [A network-related or instance-specific error occurred while establishing a connection to SQL Server](http://stackoverflow.com/questions/1391503/a-network-related-or-instance-specific-error-occurred-while-establishing-a-conne) - my guess is probably a firewall on your machine. – Bridge Sep 07 '16 at 10:09
  • So the application logs in with the current Windows logged user? Maybe a different account? – Rubens Farias Sep 07 '16 at 10:09
  • 1
    Since you have to wait for 20-30 seconds until the exception, I would highly suggest that this either due to firewall or due to domain network configuration changes. – Tomas Smagurauskas Sep 07 '16 at 10:11
  • @ Bridge: I checked the answers in this tread, but nothing helped :-( - @ Rubens The application should use the user's credentials. However, the database that I'm trying to access is one where all users in the org have access to. - @ Tomas: I tried to do this with the firewall off (both server and client), which turned out, returned the same error. – Jan Solo Sep 07 '16 at 11:29
  • @JanSolo Is your application running with an app pool account? If yes, does it have permissions on your database? – Sudhakar Sep 07 '16 at 17:02
  • The application runs with underneath the users account. – Jan Solo Sep 09 '16 at 06:24

2 Answers2

0

As per my experience in such scenarios when the inner exception says "Access is denied", it is assured that the account with which your program is running is not having access to the DB.

Suggest you to check if you are impersonating with any account and if it is having access to be DB.[In my case the password of the the account that was being used to impersonate (in the web.config) was changed and it gave me this error ]

Sudhakar
  • 136
  • 7
0

I experienced a similar problem recently. Running a specif project could not connect but other projects and applications could. I rebooted and the problem was gone. Try rebooting.

Jonny
  • 1,037
  • 7
  • 15
  • Rebooted both server & client. Still same issue. – Jan Solo Sep 07 '16 at 11:54
  • I found that the the application queried a DB on a linked server. (I didn't wrote the application, so I missed this one). On the linked server a transaction got 'stuck' blocking all other transactions on this DB. After a reboot of the linked server, things went back to normal. @ everyone: Thanks for the support! – Jan Solo Sep 09 '16 at 11:36