An Attempt to attach an auto-named database Error

前端 未结 6 765
名媛妹妹
名媛妹妹 2020-12-03 21:48

\"An attempt to attach an auto-named database for file C:\\Users\\John\\documents\\visual studio 2010\\Projects\\PAS\\PAS\\bin\\Debug//PatAddSys.mdf failed. A database with

相关标签:
6条回答
  • 2020-12-03 22:08

    Use this:

    Path.GetFullPath(yourpath_string)
    

    it will work

    0 讨论(0)
  • 2020-12-03 22:13

    Change both the current working directory and the connection string to the correct ones to solve it.

    Select the database which is located in "Server explorer" then copy the connection string as exactly seeing in its properties, then use it in the codes.

    then for the current working directory use the same path without the database's name. Solution -> properties -> debug is where the current working directory path is located at. This works for visual studio 2015.

    I use my connection string as this

    Private constr As String = "Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename=" +
            Directory.GetCurrentDirectory() + "\DBNAME.mdf;" +
            "Integrated Security=True;Connect Timeout=30;User Instance=False"
    
    0 讨论(0)
  • 2020-12-03 22:26

    i think it might be very very late BUT

    this string give me the above error

    <add name="MAB_ERP_2_0.Properties.Settings.MyConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Database\MabErp2.mdf;Integrated Security=true;"
            providerName="System.Data.SqlClient" />
    

    But if add |Data Directory| before database name then it work fine

    <add name="MAB_ERP_2_0.Properties.Settings.MyConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database\MabErp2.mdf;Integrated Security=true;"
            providerName="System.Data.SqlClient" />
    
    0 讨论(0)
  • 2020-12-03 22:27

    Actually I've crashed to this problem but I handled it easily. if your connection string is

     connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"
    

    you need to make your own localDB. first go to command prompt than write

    sqllocaldb create MyDatabase
    

    than start your database

    sqllocaldb start MyDatabase
    

    than return to VS and change your connection string to

    connectionString="Data Source=(LocalDB)\MyDatabase;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;"
    
    0 讨论(0)
  • 2020-12-03 22:29

    Try setting the User Instance property in your connection string to true. You need to add this to your connection string:

    User Instance=True
    

    Also just to be sure check again your database server as it might already contain a database with the same name.

    Hope that helps.

    0 讨论(0)
  • 2020-12-03 22:33

    FOR FUTURE HELP !!

    you've corrected it:

    private string dbPath = Application.StartupPath + "//PatAddSys.mdf";
    

    BUT just have to do one step is to use the Backslash " \ " instead of SLASH " / " so it should to be like this :

    private string dbPath = Application.StartupPath + "\\PatAddSys.mdf";
    

    &thanks this line saved me alot of work :)

    0 讨论(0)
提交回复
热议问题