C# connect to SQL server express

Discussion in 'Programming & Software Development' started by Quantum Flux, Dec 25, 2017.

  1. Quantum Flux

    Quantum Flux Member

    Joined:
    Aug 1, 2005
    Messages:
    871
    Location:
    Canberra
    Been programming causally for a few years. Mostly desktop apps using C#.

    I want to start using external databases for my software, so I'm playing with SQL server.

    Have installed Windows Server 2016 eval on a virtualbox machine, and SQL server 2017 Express.

    Created a database called testdb.

    Windows firewall on DB server is turned off.

    At the moment, I'm trying to simply open a connection to the database from my physical system -

    Code:
    using (var conn = new SqlConnection())
                {
                    conn.ConnectionString = @"Server=172.16.170.40\SQLEXPRESS;Database=testdb;Trusted_Connection=True";
                    conn.Open();
                }
    However, I just get an exception when it attempts to open the connection -
    Code:
    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
    
    Additional information: 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)
    It seems to behave as though it cant reach the server, though i am able to ping the VM.

    I've worked through a bunch of different tutorials, but few address setting up the database very much. I'm thinking I've missed something there.

    Can anyone point me in the right direction?

    Thanks
     
    Last edited: Dec 25, 2017
  2. Aich-Kay

    Aich-Kay Member

    Joined:
    Apr 20, 2002
    Messages:
    173
    Location:
    Sydney
    Start sql browser service id say

    Also specify instance name if required
     
  3. waltermitty

    waltermitty Member

    Joined:
    Feb 19, 2016
    Messages:
    531
    Location:
    BRISBANE
    Try

    Code:
    conn.ConnectionString = @"Server=.\SQLEXPRESS;Database=testdb;Trusted_Connection=True";
    Something about named pipes and binding on every interface
     
  4. Aich-Kay

    Aich-Kay Member

    Joined:
    Apr 20, 2002
    Messages:
    173
    Location:
    Sydney
    Ahh yeah enable tcpip in sql server configurator.
    Good one
     

Share This Page