Mojtaba Khooryani
Mojtaba Khooryani

Reputation: 1875

Can not connect to local address in SQL Server

Cannot create SQL connection with this connection string:

<connectionStrings>
    <add name="SchoolContext" 
         connectionString="Server=.;Database=SchoolContext;Integrated Security=True;"
         providerName="System.Data.SqlClient" />
</connectionStrings>

I get this 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

but there's no problem with this connection string:

<connectionStrings>
    <add name="SchoolContext" 
         connectionString="Server=DESKTOP-7M2F9E2\MOJTABA;Database=SchoolContext;Integrated Security=True;"
         providerName="System.Data.SqlClient" />
</connectionStrings>

Why can it not connect to local?

Upvotes: 0

Views: 149

Answers (3)

Christopher
Christopher

Reputation: 9824

This is not a C# problem, so the tag for that was wrong. The Network code does not care if the other end is on the same computer, same switch or the Voyager 2 Probe.

Proper installation and administration of a SQL Server is a totally seperate mater from programming.

And finally connection strings are a mater so complicated, there is a dedicated webpage for that.

This is either a SQL Server Administration issue (the instance you try to connect does not exists/is not allowed to take connections) or a Connection String issue (you formated it faulty). It is really impossible to tell without knowing how exactly your Environment looks, wich is something only you can realy do.

Upvotes: 0

Rahul
Rahul

Reputation: 77896

That's most probably cause you are trying to connect to a named instance called MOJTABA and not to a default instance as can be seen from your posted code connectionString="Server=DESKTOP-7M2F9E2\MOJTABA. You can as well say Server=localhost\MOJTABA or .\MOJTABA

Upvotes: 3

PSkalka
PSkalka

Reputation: 147

I would try this:

<connectionStrings>
    <add name="SchoolContext" providerName="System.Data.SqlClient" connectionString="Server=.\MOJTABA;Database=SchoolContext;Integrated Security=True;"/>
</connectionStrings>

Server=.\MOJTABA means: local machine (.), MOJTABA database server instance. The dot alone is not enough to know which instance to connect to.

Upvotes: 1

Related Questions