An attempt to attach an auto-named database for file failed...

Written by kamal on May 31, 2007
How to solve the issue of attaching an MS SQL Database to the MS SQL Server?

Explanation:

You got a task for working with ms sql database. You worked hard and finished the task in time. Now, the last step have to be done ... Publishing... yeah! Publishing your application. You need to bundle all the requierd files and applications with it (.NET Framework, SQL Server, Your References,... etc). You did that part also successfully.

Now, you want to test the published application right?. You installed it and when you run the application you got this error:

An attempt to attach an auto-named database for file C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be Opened, or it is located on UNC share.

Any idea what's happning?

There are several reasons for the above error. In most cases, it has nothing to do with the last part of the error message - "A database with the same name exists, or specified file cannot be Opened, or it is located on UNC share".

Here is some work arounds to get rid of the above error message:

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. Are you using beta versions of VWD, Visual Studio 2005 or SQL Server 2005 Express? Uninstall them and install the released versions.

6. 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 Server Management Studio, right click and select 'Properties'. Select the node 'Security' to view/change the authentication mode)

7. Make sure your connection string is correct. It must be something like below:

"Data Source=.\SQLExpress;Persist Security Info=True;Integrated Security=SSPI;Initial Catalog=YourUserId_DatabaseName"

When you use Integrated Secuiryt=SSPI in the connection string, make sure you are using in the section of the web.config and also make sure to specify a valid windows account for the "Anonymous User" access section in IIS.

Visitors/Readers Comments
(for questions please use The Forum)



Atul Kamble
We have tried it but this isn't working

28/12/2007 01:29:35 UTC

Sarath

Sir,

  is there any problem if we deploy our database i sqlexpress?

29/12/2007 00:26:48 UTC

Sarath

sir,

  I have created a database in sqlserver 2005 and ihave completed the application

 done in c#.net.Now i want to make the application exe.How can i make exe with

sqlserver 2005..now iam doing it by making it sqlexpress.how can i Deploy sql sever

2005 with the application or is there any problem using sqlexpress 2005..

Any one pls send details to

29/12/2007 00:33:51 UTC

himanshu
hey Sarath i have encountered the silirar problem of making exe of c# application, i have to attach Sql server data base

07/04/2008 09:07:28 UTC

Harpreet

Hi there,..

I also created an application using the VS2005 and language c#, my application   uses sql database backend. Now i want to create an exe of this application and give it to my freind on a cd so that he can install it on his computer and run it... any idea how do i go about creating an exe with the database attached.. 

any help would be beneficial..

regards

Harpreet

17/04/2008 09:10:27 UTC

Maysam
Great Job! my connection syntax was wrong

16/07/2008 01:11:05 UTC

Pavan

I really frustrated with this error for more than two days.I tried all the resolutions posted on the net for this error,but no luck. First i really got confused, why its looking for aspnetdb.mdf which is not the actual mdf file name for the sqlserver instance/express that is installed on my machine.

 

Finally, i debug the application.Some how i got the solution.This error occurs when you retrieve the connection string from the web.config using index.Initially i tried below.

 

ConfigurationManager.ConnectionStrings[0].ConnectionString.ToString()

 

When i tried above, i got the above error.After removing the index and accessing the connection string with name its worked for me.I just changed the above line like below.

 

 

ConfigurationManager.ConnectionStrings["MyConnectionString "].ConnectionString.ToString()

 

Where MyConnectionString is the name of the key given to the connection string in the web.config file.

 

This is how i resolved the error.But there may be some other reasons for getting error.I am nt sure about that.But try this option too.
 

01/12/2008 03:11:55 UTC




Add your Comments

Name:  
Message:


Advertise Here
For more details
Contact Us

Advertisments