I am using following code to attach my database to SQL Server. The problem is if I create a new file and attach it through my code but when I detach the file using SSMS and again run the same code, it gives error.
Error is:
Unable to open the physical file “e:\dbForATTWithPWD.mdf”. Operating
system error 2: “2(error not found)”
The code is:
Dim conn As New SqlConnection("Server=MyHomeServer\SQLExpress;Database=master;Integrated Security=SSPI")
Dim cmd As New SqlCommand("", conn)
cmd.CommandText = "CREATE DATABASE MyHomeWithPWD ON ( FILENAME = 'e:\dbForATTWithPWD.mdf' ), ( FILENAME = 'e:\dbForATTWithPWD_log.ldf' ) FOR ATTACH"
conn.Open()
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Dispose()
Why is it happening. All the permissions are the same even first time.
Also what should be the connection string if I need to use DB with uid sa and pwd abc123 ?
Thanks
Try to making sure that nothing else is accessing that mdb file after you detach it. I would probably shutdown that SQLServer instance and restart. Also, I’d make sure that you could detach and reattach in SSMS.
For your connection string try this: “Server=YOURSVR;Database=DB;Trusted_Connection=False;User ID=sa;Password=abc123;”