Tommy B.
Tommy B.

Reputation: 3659

SQL Server 2008 connectionString question

I'm trying to connect to my SQL Server 2008 database with ASP.NET MVC 3. Here is my connection string:

  <connectionStrings>
    <add name="AppConnectionString"
         connectionString="Data Source=(local);Initial Catalog=DatabaseName;User Id=UserName;Password=PassWord;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

Any idea of what's wrong?

When I try to query something in my DbContext I get this exception:

  • $exception {"The provider did not return a ProviderManifestToken string."} System.Exception {System.Data.ProviderIncompatibleException}

  • InnerException {"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)"} System.Exception {System.Data.SqlClient.SqlException}

Could someone help me please?

Thanks

Upvotes: 6

Views: 25274

Answers (7)

Utkarsh Dhande
Utkarsh Dhande

Reputation: 1

        SqlConnection [object name];
        [object name] = new SqlConnection("Data Source=Server_address/name;Initial Catalog=myDatabase;Integrated Security=True");
        [object name].Open();

Note: The square brackets which is shown should not be putted, only object name have to be placed. Server_address : Your Computer name. myDatabase : your Database name.

Upvotes: 0

Paul Sasik
Paul Sasik

Reputation: 81557

Besides (local) try (localhost) or 127.0.0.1 (loopback address - and no place like ;-)

Also a single period will "." resolve to the local machine.

Your connection string and the advice above assumes that you are trying to connect to an unnamed, default instance. If your server instance is named then you need to include that as part of your conn string, like: .\ServerName

More great info can be found at: http://connectionstrings.com

Upvotes: 2

Gareth Oates
Gareth Oates

Reputation: 397

If you look at the InnerException it's possible that what you actually have is a Login issue.

What I had to do to fix this was, change the account the DefaultAppPool runs as, to NetworkService (go to Advanced Settings and change Process Model, Identity) and then pick an account which has access to your database.

Assuming of course your app is running in IIS using the DefaultAppPool.

I made it NetworkService and granted the NT AUTHORITY\NETWORK SERVICE user access to my existing database.

This then allowed me to connect. My case might be specific but I thought I'd share just in case.

Upvotes: 1

Atle S
Atle S

Reputation: 289

I had a similar problem, and these are the steps that helped me:

1) Your application must have an App.config containing a connectionstring named with the same name as your class that inherited from DbContext. In my case "TestEF_CF.ProductContext".

<add name="TestEF_CF.ProductContext" connectionString="Data Source=localhost;Initial Catalog=ef_cf_test;Integrated Security=True" providerName="System.Data.SqlClient" />

2) The database cannot be created before you start to use it. Just set the Initial Catalogue to the name you want Entity Framework to create when it autocreate the database.

See my stackowerflow question for more information if you need it: Entity Framework 4.1 Code First Freeze

Upvotes: 1

Sem Vanmeenen
Sem Vanmeenen

Reputation: 2151

That error means that either the name of your Data Source in your connectionstring is wrong or that your sql server is not configured to allow remote connections : see here how to fix it.

Upvotes: 3

Karel
Karel

Reputation: 2212

Can you ping the server?

You can create a new empty text file, rename it to test.udl, double click on it and create your connection string that way. (A dialog will open and you can select provider, server, database etc).

also, have a look at www.connectionstrings.com for example connection strings

There is also the instance to take into account. eg if you use sqlexpress it may be (local)\SQLEXPRESS

Upvotes: 2

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55529

First check if your mssqlserver services are running fine. Just run net start mssqlserver in your command prompt.

Then try changing the connection string Data Source=(local) to Data Source=.

All the above is assuming that you have sql server installed in your local machine.

Upvotes: 3

Related Questions