Join 300,363 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,440 people online right now. Registration is fast and FREE... Join Now!
System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file D:\Coding\Databases\MCR_Testing\MainDB.sdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
In your connection string you have MainDB.sdf, what is a sdf file?
1. Open SQL Server Management Studio and attach the .mdf file manually to the SQL Server. (Select the 'Databases' node, Right click and select 'Attach')
Now change your web.config and change the connection string to point to the pre attached database instead of auto attach.
2. SQLExpress creates a directory per user in "c:\Documents and Settings\[user name]\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS" to store some information. Delete this directory and try again.
3. The windows account "ASPNET" must have write permission on the folder. If it is a Windows 2003 machine, provide write access to the account "NETWORK SERVICE" on the folder, instead of account ASPNET.
4. You may want to disable the "User Instance=true" and try.
5. User Instance=True will work only if your SQL Server 2005 Express is using Windows Authentication mode. It will fail if you are using mixed mode. (Select the computer name in the SQL Serve r Management Studio, right click and select 'Properties'. Select the node 'Security' to view/change the authentication mode)
6. Make sure your connection string is correct. It must be something like below:
When you use Integrated Secuiryt=SSPI in the connection string, make sure you are using <identity impersonate="true" /> in the <system.web> section of the web.config and also make sure to specify a valid windows account for the "Anonymous User" access section in IIS.