0

I created an MVC3 Internet application and changed the default connection string to work with .\SQLSERVER instead of the default .\SQLEXPRESS and get following 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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)

I tried creating an empty MVC3 application as well, which has no connection string by default and added mine with the same result. The SQLSERVER instance is running and I can connect to it from management studio using windows authentication. Any connection string that I use gives same error, I cannot figure it out:

<connectionStrings>
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" connectionString="data source=.\SQLSERVER;" providerName="System.Data.SqlClient" />
</connectionStrings>
3
  • Are you able to connect from SQL Management Studio? Commented Dec 22, 2011 at 16:07
  • @torm, yes i can, with no problem at all. Commented Dec 22, 2011 at 16:09
  • Were you able to resolve your problem ? Commented Dec 27, 2011 at 18:30

4 Answers 4

1

To connect to your local default instance (i.e. SQLSERVER service name), for Data Source=..., use one of the following:

  1. 127.0.0.1
  2. localhost
  3. (local)
  4. .

EDIT

Your connection string should resemble this:

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Sign up to request clarification or add additional context in comments.

2 Comments

@Chuchelo are you sure you have SQL Server installed and a default instance service started and listening?
@Chuchelo see my edit, your connection string should resemble this. I notice in your original post/question it isn't full. Can you try that?
0

Checkout the different connection strings you can use with SQL Server. For example if you have a SQL Server installed on your local machine you could use the following connection string:

Data Source=127.0.0.1;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Obviously you will need to adjust the name of the database, the username and password to connect to.

If you want to use Windows Authentication to connect to the server:

Data Source=127.0.0.1;Initial Catalog=myDataBase;Integrated Security=SSPI;

1 Comment

tried all what you suggested and same error. It seems that I just cannot connect to the sql server and the problem is not in the type of connection or username or password. Maybe its due to Entity Framework ? Btw I'm running the project from VS without IIS application setup. I'm new to this, so not sure if it matters.
0

Your connection string needs to look something like this:

<add name="NameOfConnection" connectionString="Data Source=localhost;Initial Catalog=OMSCING;Trusted_Connection=Yes" providerName="System.Data.SqlClient" />

Where you can replace localhost with a hostname or leave it like that if the server's local.

2 Comments

still same issue, probably the issue is not with connection string, but I just cannot figure out where.
connecting locally would mean you have all the configuration done correctly... what about connecting remotely? Try opening port 1433 and connecting from another computer.
0

Perhaps I had to mention that I was using EF with code first approach, but the problems was due to the name of the connection string which should be the same name as your Context which you derive from DbContext, e.g.:

<connectionStrings>
<remove name="ApplicationServices"/> 

<add name="MyContext" 
     connectionString="data source=localhost; Initial Catalog=CoolCars;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

as well make sure you remove the default machine.config connection string which is for .\SQLEXPRESS

Comments

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.