3

Solution: Add the directory to your connection string in the app.config file and the Settings.setting file in the properties section of your project. My working connection string ended up being <Value Profile="(Default)">Data Source=(LocalDB)\v11.0;AttachDbFilename=F:\hi\prgrm\ProgramName\Database1.mdf;Integrated Security=True</Value>

Once I run my program I get the following error:

An attempt to attach an auto-named database for file F:\Graded unit 2\SimplyRugby\LollipopUI\bin\Debug\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

The method that makes the error happen:

public bool CheckUsername(string username)
{
    var usernameResult = (from person in dbContext.Persons
                            where (person.Username == username)
                            select person.Username).FirstOrDefault(); 
                          //stores username if a username is found

    return !(string.IsNullOrEmpty(usernameResult));
    // if no correct user found from query return false else true
}

After some research apparently it's that the connection string is wrong. I had a little play with some suggestions online but I'm not too sure what is incorrect and how to fix it so I've been going around in circles for the past five or so hours.

My app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="LollipopUI.Properties.Settings.Database1ConnectionString"
             connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

Thank you for your time.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
Zain
  • 1,246
  • 4
  • 15
  • 26
  • Possible duplicate: [Attempt to attach an auto-named database for .mdf file failed](http://stackoverflow.com/questions/8747673/attempt-to-attach-an-auto-named-database-for-mdf-file-failed) – stuartd May 13 '16 at 12:06
  • Add the logical database name so the file is recognized and not attached a second time ;Database=myDatabase; – Steve May 13 '16 at 12:13

3 Answers3

0

Try to make User Instance property as true like

User Instance=True

in your connection string.

You can also refer to this related thread which says that your connection string should looks something like this:

<connectionStrings>
    <add name="Sales_DBEntities" 
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'" 
        providerName="System.Data.EntityClient" />
</connectionStrings>
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Hi @Rahul Tripathi, I tried that already and it says "The user instance login flag is not allowed when connecting to a user instance of SQL server, The connection will be closed." when I change the user instance property to true. – Zain May 13 '16 at 12:25
0

I have faced that situation and I resolved it like this. Go to Setting of DataBase and set option **Copy to output Directory** to **Copy if Newer** the problem will solve

Ahmad
  • 770
  • 9
  • 21
-1

Late response is better than no response at all:

The issue was that my connectionString wasn't mapped to the database location.

Edit: I'm not sure what the path I used was as it was so long ago but if you have this issue try using a full hardcoded path to the .MDF file. After you've confirmed it works then you can start fiddling around with it.

Zain
  • 1,246
  • 4
  • 15
  • 26