I am using the following VB code to attach my DBs to server.
Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Database='" & TextBox1.Text & "';Connect Timeout=30")
cnnConnection.Open()
But it gives an error:
Unable to open the physical file “D:.Net Programs\SQLDBAttachExperiments\SQLDBAttachExperiments\bin\Debug\datafiles\dbSQLtest.mdf”. Operating system error 5: “5(error not found)”. Cannot attach the file ‘D:.Net Programs\SQLDBAttachExperiments\SQLDBAttachExperiments\bin\Debug\datafiles\dbSQLtest.mdf’ as database ‘dbSQLtest’.
How to fix it?
Thanks
It may be due to the fact that the file is already opened due to some error while establishing the connection so establish the connection like below