Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using following code to attach my DB 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:
VB
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
Posted
Updated 11-Feb-12 7:21am
v2
Comments
André Kraak 11-Feb-12 13:22pm    
Edited question:
Added pre tags

1 solution

www.connectionstrings.com[^] is the place to go to build connection strings. Are you SURE the file is still in the location you're stating ? The error message says it is not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900