0

I've been trying to publish my application so I can install it on other computers but I always end up with an "SqlException" when trying it on the VS editor and when running it on the other computer

to make things clear I made the database using SqlServer and I attach it to the VS editor until now everything is going well and the application is running on the editor without any exceptions but to make the application working on another computer I needed to copy the (mdf file from the SqlServer directory and paste it in the |datadirectory| of the project ) and of course I changed the connectionString dataSource to point to the mdf file and here where the exceptions begin to rise

The message of the exception says:

An attempt to attach an auto-named database for file C:\Users\xXx\Desktop\Projects\Master Maint\Master Maint\bin\Debug\GMAO.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

when I add the Initial Catalog=test in the connection string the exception message becomes:

"Cannot attach the file 'C:\Users\xXx\Desktop\Projects\Master Maint\Master Maint\bin\Debug\GMAO.mdf' as database 'test'."

PS 1: It's not a permission issue cause I gave full access to the file from the advanced installer

PS 2: if there is the other way to run the database application on the other computer, I'm listening.

<connectionStrings>
    <add name="connString2" connectionString="Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\GMAO.mdf ;Integrated Security=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • 3
    I suggest googling on `LocalDB` and read up about it. – GuidoG May 09 '19 at 14:48
  • If it's a `SQL Server` , add the connection string to it to the config file (should include IP address) and then have the program read it from the config file.---- Unless it's a `LocalDB` as Guido stated – Jaskier May 09 '19 at 14:49
  • Possible duplicate of [An attempt to attach an auto-named database for file ....database1.mdf failed](https://stackoverflow.com/questions/12566036/an-attempt-to-attach-an-auto-named-database-for-file-database1-mdf-failed) – ADyson May 09 '19 at 14:57
  • it's a LocalDB in the form of **mdf** file attached with the project and even further it's installed with the project ,but as I mention the problem is in the access process **An attempt to attach an auto-named database for file failed** – Tuni Games May 09 '19 at 15:01
  • For anyone to help you you're going to have to post more code than that. It's impossible to diagnose from just what you've said. – BanksySan May 09 '19 at 15:16

1 Answers1

0

Thank you all , I'm not gone delete the question cause may others need it , but finally I found the answer

1 - the first bug was because the database file is not Categorized and that was solved by adding the Initial Catalog=test

2 - second the database file is named same us the the real database used in the project and that was solved by renaming the file

3 - third the log file that responsible of creating the database was not attached and that solved by adding the log file to the directory

  • fyi: if you need to attach to a database during installation, please take a look on the following article which may be useful to you: https://www.advancedinstaller.com/user-guide/qa-attach-to-database.html – J.Tribbiani May 10 '19 at 12:49