0

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)

Here's the function that throws the error

public void AddDatos(LectorDatos ld, int sensorId)
{
        using (Model.CEREBROEntities context = new Model.CEREBROEntities())
        {
            Motor m = (Motor)ld;

            Model.Motor motor = new Model.Motor()
            {
                SensorID = sensorId,
                Date = DateTime.Now,
                PresionAceite = m.PresionAceite,
                TempMotor = m.TempMotor
            };

            context.Motor.Add(motor);
            context.SaveChanges();

            return;
        }
}

The connection string was automatically generated with an ADO.NET Entity DataModel which uses a working DB connection (pressing the "Test Connection" button gives positive feedback). I gave the model the name CEREBRO and called the connection string CEREBROEntities.

<configuration>
    <configSections>
        <section name="entityFramework"  
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="CEREBROEntities" 
             connectionString="metadata=res://*/Model.CEREBRO.csdl|res://*/Model.CEREBRO.ssdl|res://*/Model.CEREBRO.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CASCA-PC\SQLEXPRESS;initial catalog=CEREBRO;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
             providerName="System.Data.EntityClient" />
    </connectionStrings>
    <entityFramework>
        <defaultConnectionFactory  type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
            <providers>
                <provider invariantName="System.Data.SqlClient" 
                          type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            </providers>
    </entityFramework>
</configuration>

I can also connect to the server using Microsoft SQL Server Management Studio with windows authentication and SQL Server authentication, but none of them worked on Visual Studio.

Using the command line sqlcmd -L lists the server

Things i tried:

  • The server allows remote connections.
  • Disabled both AntiVirus and Firewall to try.
  • Both SQL Server and SQL Server Browser services are running.

Any ideas on what could be causing the problem or reliable ways to test a connection string?

2
  • 3
    Debugging, check the runtime value of Model.CEREBROEntities.Database.Connection.ConnectionString. Commented Nov 2, 2018 at 13:25
  • This was really helpful, from what i can see it's taking the value of the connection string of another person working on the project. Commented Nov 2, 2018 at 13:33

1 Answer 1

1

It's solved. There was another App.config in another layer which was modifying the connection string i was using.Thank you to David Browne - Microsoft for guiding me to the right answer.

Sign up to request clarification or add additional context in comments.

1 Comment

In 2 days, since until then SO won't let me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.