Unable to connect to Remote SQL Server Application from C# Application

  • Hi there

    We have a remote SQL Server instance on a VM , that we can connect to successfully from SQL Server Management Studio on my local machine.

    However I am having problems connecting to this database from a c# application

    Now my connection string is as follows:

    string ConnectionString = "server=METRODEMO\\SQLEXPRESS;Database=Logger;Trusted_Connection=True;";

    I have also checked and the propeties on the remote SQL Server are as follows:

    SQL Native Client 11.0 Configuration (32 bit)

    -->Client Protocols

    Shared Memory - Enabled

    TCP/IP - Enabled

    Named Pipes - Enabled

    Now in SQL Server Network Configuration -- Protocols for SQL Express

    The properties for IP Addressess has TCP Port set to 1433 in the IPAll section

    (please see attachd screenshots)

    However when i try to connect , I get the following error:

    System.Data.SqlClient.SqlException

    HResult=0x80131904

    Message=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)

    What could I be doing wrong?

    Attachments:
    You must be logged in to view attached files.
  • Few things I would try, but the first would be to connect to the server using the FQDN rather than the friendly name.

    I would also make sure your connection string is correct.  If you copy-paste the value in "server" into your server name in SSMS (not typing it in as if there IS a typo, you may correct it absentmindedly) and see if you get errors.  You could also try including the port number when connecting as it looks like you are listening on port 1443.  If the SQL Browser service isn't running on the server, you MAY run into issues connecting without the port number.

    Just a few things to try.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Hi Brian

    Thanks for your reply. I figured this out as follows:

    I changed the connection string to the following:

    private string defaultConnectionString = "server=METRODEMO\\SQLEXPRESS,1433;Database=Logger;Trusted_Connection=True;";

    All I need to do is to put the port number in seperated by a comma

     

     

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply