0

I had created a Application Winform and use connection with local file put same path file.exe. User connection string:

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='DataManagement.mdf';Integrated Security=True;Connect Timeout=30 => This is working a many days ago and I had to release.

Now, I open source with Visual Studio with same device which Build source before, and add some functions, not change any about of connection string. Then, I run and a messagebox appear:

Cannot open database "...\BIN\DEBUG\Database.MDF" requested by the login. The login failed. Login failed for user 'ASKNKO\ASKNKO'.
enter image description here

-I try to run file.exe which release before, It's still working.
-I copy two file .mdl and .ldf from release to path debug and run file.exe => It's worked. But this database have not update some tables like new Database in debug has error
-I use SSMS to Attach file mdf which work fine to add tables, then I Detach this database from SSMS tool to close connection. And I run in visual, the message error appear again like above
-Why open file in SSMS tool with Window Authenticate is OK, but not in Visual Studio ?

How can I fix this ?

Thanks.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • 1
    Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface Dec 13 '21 at 09:51
  • @Charlieface I had changed Instance as your url suggested, but not worked :( – Hồ Kim Long Sơn Dec 13 '21 at 14:48
  • What is your current connection string? Did you attach the database properly using `CREATE DATABASE FOR ATTACH`? – Charlieface Dec 13 '21 at 15:23
  • @Charlieface my connection string is: (LocalDB)\MSSQLLocalDB;AttachDbFilename='DataManagement.mdf';Integrated Security=True;Connect Timeout=30. Yes I had attach the database after modify to same path with file.exe – Hồ Kim Long Sơn Dec 14 '21 at 16:29
  • So why use `AttachDbFilename` if you have already attached? Just use `Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=YourDB;Integrated Security=True` – Charlieface Dec 14 '21 at 17:45
  • I think because version of SQL Server LocalDB 2016 different SQL Server 2019, I had try to install SQL Server LocalDB express 2019, and change connection string from Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=YourDB;Integrated Security=True to Data Source=.\SQLEXPRESS;Initial Catalog=YourDB;Integrated Security=True It's worked, but I had attached the file.mdf in SSMS, then error again: cannot logon with default user... – Hồ Kim Long Sơn Dec 14 '21 at 19:36
  • SQL Express and LocalDB are not the same, LocalDB is a stripped down version of Express, and is installed separately. Since you have reinstalled it (or connected to a different instance) you will need to reattach again. You also need to ensure you have permissions, by default only Administrator has permissions on SQL Express – Charlieface Dec 14 '21 at 20:17

0 Answers0