I\'m trying to connect to an MDF. I\'ve even gone to the lengths of re-installing sql server express entirely (it is now the only flavor of SQL installed on my box, where p
Have you tried to connect to the SQL Server instance by using SQL Management Studio?
If this also doesn't allow you to connect then it could be because the SQL service is not starting correctly.
Verify that the service is running by checking the services in the control panel.
As noted by others, deleting the contents of directory: c:\Users\\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS solved it for me. A note that may help others, on Windows 7, browsing the c:\users\username dir from document browser does not show the the AppData folder which threw me off for a while (as I thought I did not have the AppData dir) until I discovered that it does indeed exist (but does not show in the document browser in Windows 7), you just have to type in the full path name to get to it.
Even despite above answers solved the problem many people, I m still finding these falling short of the essence of the problem. Closest to it is the answer by @sohaiby above. But it wrongly refers to using Management Studio.
The error message on top of this topic tells very clearly that the problem is with generating a user instance. What instance is and how it is used is described here in every detail: https://msdn.microsoft.com/en-us/library/ms254504(v=vs.110).aspx
Personally, I encountered this problem when switching DB connection from windows authentication mode to SQL Server authentication. I solved it by just modifying a part of connection string to: "User Instance=false;" instead of "User Instance=true;", which worked fine with windows authentication.
After I changed to "User Instance=false;" my connection worked fine without any additional manipulations. I cannot insist that it will work or will be suitable in all scenarios. However, I will definitely recommend trying it before other drastic methods described above, such as erasing SQL server work directories.
I have faced the issue when i was using SqlLocalDB and SqlExpress. Cause of the issue: When I connect to localdb from Visual Studio, a LocalDB instance is started for it and runs as our Windows account. But when Web Application, running in IIS as "Network service" or "AppPoolIdentity", is connecting to LocalDB, another LocalDB instance is started for it and is running as "Network service" or "AppPoolIdentity". As a result, even though both Visual Studio and Web Application are using the same LocalDB connection string, they are connecting to different LocalDB instances. Obviously the database created from Visual Studio on our LocalDB instance will not be available in Web Application's LocalDB instance. Solution: So, here i have list out the solutions for both cases (i.e) SqlExpress and LocalDB If we are using LocalDB as the datasource:
<processModel identityType="NetworkService" loadUserProfile="true" setProfileEnvironment="true" />
(Here "MyLocalDB" is my local db instance. You can find the same by executing the following command in CMD.exe in admin mode SqlLocalDB.exe) Thus, my connection string is as follows: <add name="DefaultConnection" connectionString="Data Source=(localdb)\ .\MySharedLocalDB;AttachDbFilename=|DataDirectory|\aspnet-DemoSite-20200716010415.mdf;Initial Catalog=aspnet-DemoSite-20200716010415;User ID=sa;Password=welcome123;User Instance=false"
If we are using SqlExpress as the datasource:
1.Make sure that the connection string has proper value as below: <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-DemoSite-20200716010415.mdf;Initial Catalog=aspnet-DemoSite-20200716010415;User ID=sa;Password=welcome123;User Instance=false" 2.As highlighted above Jane, delete old files which located in C:\Users<userName>\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
I phase this problem with my mdf file in windows form application and i do just restart my computer and my problem is solve.