C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

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!




Attaching Database

 

Attaching Database, Canīt get it work :(

b.ihde

1 Jul, 2009 - 10:59 AM
Post #1

D.I.C for life
Group Icon

Joined: 29 Sep, 2008
Posts: 973



Thanked: 30 times
Dream Kudos: 75
My Contributions
Hi,

i recieve this Error:

QUOTE
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.


My Code:

CODE

AppDomain.CurrentDomain.SetData(
     "DataDirectory", @"D:\Coding\Databases\MCR_Testing\");

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     @"Data Source=.\SQLExpress;" +
     "Integrated Security=SSPI;" + "User Instance=True;" +
     "AttachDbFilename=|DataDirectory|MainDB.sdf;";
conn.Open();


I have tried so many things and nothing worked.. sad.gif

Thanks!

Ben

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Attaching Database

1 Jul, 2009 - 11:20 AM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,713



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
First go into Services and make sure SQL Browser is running and that SQL Server is running. If that doesnt work then post back here
User is offlineProfile CardPM
+Quote Post

b.ihde

RE: Attaching Database

1 Jul, 2009 - 11:22 AM
Post #3

D.I.C for life
Group Icon

Joined: 29 Sep, 2008
Posts: 973



Thanked: 30 times
Dream Kudos: 75
My Contributions
Yes, both are running..
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Attaching Database

1 Jul, 2009 - 11:36 AM
Post #4

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,713



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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:

"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 <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.
User is offlineProfile CardPM
+Quote Post

b.ihde

RE: Attaching Database

1 Jul, 2009 - 11:42 AM
Post #5

D.I.C for life
Group Icon

Joined: 29 Sep, 2008
Posts: 973



Thanked: 30 times
Dream Kudos: 75
My Contributions
QUOTE(PsychoCoder @ 1 Jul, 2009 - 07:36 PM) *

In your connection string you have MainDB.sdf, what is a sdf file?


Itīs a compact database file..

I already did some things you said. Maybe it is just because it is a .sdf file..
First i ll try to attach a mdf file, so i know more..

Thanks!


User is offlineProfile CardPM
+Quote Post

b.ihde

RE: Attaching Database

1 Jul, 2009 - 12:17 PM
Post #6

D.I.C for life
Group Icon

Joined: 29 Sep, 2008
Posts: 973



Thanked: 30 times
Dream Kudos: 75
My Contributions
After changing it to a .mdf file, it works!

Thanks again!
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Attaching Database

1 Jul, 2009 - 12:21 PM
Post #7

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,713



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
No problem, glad I could help smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:36PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month