简体   繁体   中英

Connection of local database error in Visual Studio 2013

I have an error in Visual Studio 2013. I use SQL Server 2017 and Visual Studio. I want to create a local database in my asp.net project. The problem is that when I connect to the local database with application in web config file, connection is created but when I try to open database I get an error

The attempt to attach to the database failed with the following 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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist. )

This is my connection string

<connectionStrings>
    <add name="Db" 
         connectionString="Server=(localdb)\MSSQLLocalDb;Integrated Security=true;AttachDbFileName=|DataDirectory|\cmsShoppingCart.mdf;"
         providerName="System.Data.SqlClient" />   
</connectionStrings>

I also read many articles and using techniques like enable SQL Server etc but nothing would be happen.

Thanks in advance if you help me out.

something is not correct in your connectionstring. First Try to create your connection using Visual Studio Tool :

* Tools -> Connect To DataBase *

In DataSource Selection, Select " MS SQL Server " And Click Continue, FilL The Next Page With Your Database Information And After That, Test Your Information Using " Test Connection " Button. If It Says "Test Connection Succeeded", Click On " Advanced " Button And You Will See Correct "Connection String" For Yourself at the End Of The Page now Opened.

I am thinking of three scenarios that you might be in :

  1. You are trying to connect to the Wrong Instance of SQL Server( check here for more )
  2. You don't have the SQL Database engine installed (Instead you may have only installed SQL Server Management Studio, so download the version you want accordingly from Microsoft site)
  3. The SQL Server services are not running ( more about it )

You could also use the SQL server express localDB, but first you need to find out the version of it in order to use its exact instance.

Say, if the version is 11.0 then your instance would be (localDB)\\v11.0

To find the version of installed localDB Go to this directory - C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\SqlLocalDB.exe (110\\120\\130.. are the versions)

Run this command to create and start an instance of localDB(if localDB !Exists)

"C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe" create "v13.0" 13.0 -s

Now go to SSMS or VS Server explorer connect to localDB and use an instance which for the above example would look like : localDB\\v13.0 and use windows authentication to access it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM