how to attach db at app startup and detach and end

seco

Well-known member
Joined
Mar 4, 2007
Messages
66
Programming Experience
Beginner
Hi
i have mdf file i want to attach this file to the sqlserver 2005 and when closing the program the database is detached
how to do this by sql atatements and stored procedures?
i use this

ExecuteNonQuery("sp_attach_db('hospital','hospital.mdf','hospital.ldf')")

it gives me error message says error syntax near hospital

thanks in advance.
 
Last edited:
i fix it and do te following
i connect to to master database and write the following

Dim path As String
path = My.Application.Info.DirectoryPath

pre_connect.ExecuteNonQuery("sp_attach_db 'xx','" & path & "\xx.mdf','" & path & "\xx_log.ldf';")

it gives me error message says:-
"Directory lookup for the file "C:\Documents and Settings\mokhtar\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\xx.mdf" failed with the operating system error 5(error not found)."

any help

thanks in advance.
 
i use this connection string
"Server=.\SQLExpress;AttachDbFilename=" & My.Application.Info.DirectoryPath & "\Hospital.mdf;Database='Hospital';Trusted_Connection=Yes;"

and it successed
but when i try t oget or deal with data from this attached database it gives me error message says:-

"Cannot open database "Hospital" requested by the login. The login failed.
Login failed for user 'COMPUTER\mokhtar'."

any help?
thanks.
 
Last edited:
whenever I've done data access, as per the DW2 link, using an MDF file it's worked without all this hassle! the path to the db is relative and attach/detach works automatically..
 
Back
Top