Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Windows Form project in VS2015 that uses a local database file (.MDF).
The code has been working for some time, but recently I was having a problem restoring from a backup file. I decided to uninstall SQL Server (different versions) and do a fresh install of SQL Server Express 2016 SP2 (Advanced version).

The install went okay and I did include LocalDB which is needed to connect local .MDF database files. Under 'Programs and Features' I can see several Microsoft SQL Server 2016 items, including LocalDB.

Now, for some reason I cannot connect to the local database. In VS, in Server Explorer, I try to add the .MDF file to 'Data Connections'. In the 'Add Connection' dialog I select 'Microsoft SQL Server Database File' as the source. Then I browse to and select the .MDF file. I use Windows Authentication (which I choose during SQL Server install). Clicking on 'Test Connection' produces the 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: SQL Network Interfaces, error: 0-
[x89C501181]) "

I believe the problem is something with SQL Server installation because the installed version of my project also cannot connect to the database. Could it be a Windows Registry issue?

Would very much appreciate help.

What I have tried:

I did a fresh install of SQL Server Express 2016 SP2 (Advanced version).
I verified 'LocalDB' was included in the installation.
Tried to connect to a known working .MDF database file in Visual Studio 2015,
but always get the error message.
Updated 1-Jul-20 2:53am
Richard MacCutchan 24-Jun-20 7:24am    
It sounds like Visual Studio does not recognise that the file should be accessed by SQL Express, and is still trying to connect to a remote server. Check the properties of the database to see if it can be changed.
GenJerDan 25-Jun-20 3:25am    
Where is the MDF located, and are permissions on that location set correctly?
Member 11298827 25-Jun-20 9:06am    

The file is located in the project folder, and yes, permissions are set correctly. Error message seems to indicate server cannot be found. This all started when I uninstalled SQL Server and then did fresh install of SQL Server Express 2016 (with LocalDB). My application, which is installed on the same machine, also will not connect. I'm thinking it must be a corruption in Windows Registry, but I have no idea how to troubleshoot that. In Regedit I see entries for:

SQL Server
SQL Server 2005
SQL Server 2012
SQL Server 2014
SQL Server 2016
SQL Server 2017
SQL Server Local DB
SQL Server Native Client 11.0

I had 2017 installed at one point, but uninstalled it. 2014 is also no longer installed. I don't want to start messing with the registry unless I know exactly what I'm doing, and I don't.

Update - The default instance of LocalDB was set to V12.0 (2014 version) which prevented me from connecting to database that was V13.0

But then I could not figure out how to change the default instance to V13.0. Tried using command line utility SqlLocalDB.exe but it only gave me "Format Error".

Finally, I went into "Programs and Features" and selected Sql Server 2016 LocalDB and did a "Repair". That seems to have fixed it.
Share this answer

Thanks for your reply. I'm not really sure what you mean by "check the properties of the database .." If I look at the properties of the .mdf file, the type is "SQL Server Database Primary Data File".

Also - I cannot create a new database file from Server Explorer using "Create New SQL Server Database" under "Data Connections". The dialog that comes up has a picklist of servers, but that list is empty. It's as if VS can't see any servers.
In Server Explorer, under 'Servers' I see my PC's name, and under that I can see a bunch of services, including -

SQL Server Browser
SQL Server CEIP service (SQLEXPRESS)
SQL Server Launchpad (SQLEXPRESS)
SQL Server Reporting Services (SQLEXPRESS)
SQL Server VSS Writer

I think the problem is not in VS because, like I said, my application (running outside VS) that accesses the same .MDF file, also does not work, and it did work before I did the uninstall and reinstall of SQL Server Express 2016.
Share this answer
Richard MacCutchan 24-Jun-20 10:16am    
Sorry, I am not an SQL Server expert so I have no further suggestion. All I can guess at is that your uninstall of SQL server has broken some link(s) somewhere.
Member 11298827 24-Jun-20 10:20am    
Thanks anyway for trying!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900