i m final year student of computer engineering and there is problem for me in my final year project.
i want to attach databases to sql server dbms from one of the form of my vb.net application. the idea is that a want to have a button name "Browse" when i click on it brows me to sqldatabases now when i select any sqlserver database file, it automaticall attach to sql server dbms.

inshort i want to attach databases with dbms from my application not from using entrprise manager of sqlserver dbms.

Can you please explain a bit more of what exactly you mean by "attach" to SQL Server.

I mean if we have a database file e.g data.mdf so if we want to make some some transactions in that database we must have to attach this database to sqlserver. i.e we have to go to ENTERPRISE MANAGER than goto databases and then right click on databases then will go to attach database. so a wizard will start to attach that file to dbms. now i want to do the same funtionalities from my application build in vb.net. so i dont have to use sqlserver to attach database.

Yes--it can be done through VB.Net. You will have to use SQL system stored procedures - sp_detach_db to detach the mdf and ldf file from the server. Once the files are detached-you can copy paste then to a folder. Then use sp_attach_db to attach the mdf and ldf files to the other server and remember to attach the mdf and ldf files on your server too--else the DB will not be available.

You must connect to the SQL Server master database using SQLConnection object and run the stored procedures using SQLCommand object.

Yes--it can be done through VB.Net. You will have to use SQL system stored procedures - sp_detach_db to detach the mdf and ldf file from the server. Once the files are detached-you can copy paste then to a folder. Then use sp_attach_db to attach the mdf and ldf files to the other server and remember to attach the mdf and ldf files on your server too--else the DB will not be available.

You must connect to the SQL Server master database using SQLConnection object and run the stored procedures using SQLCommand object.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Thank u sir!
Sir which methond of sqlcommand object is used to run the above mentioned store procedure?

sir let we have lms.mdf file store in c:/database folder and it is not attach to sqlserver. and we want to attach it through sp_attach_db store procedure. how i will do that. ie

1: conn.connectionstring = ?
2: conn.open
3: cmd.CommandType = CommandType.StoredProcedure
4: cmd.CommandText = "sp_attach_db "lms' "
5: cmd.connection=conn
6: cmd.executenonquery

sir in above code line number 4 gives error also what to write in lin 1.

The Connection string is the usual connection string to the database server you want to attach. For example

"Data Source=SQLSERVERNAME; INITIAL CATALOG=master; uid=sa; pwd=sa;".

.You need to add physical path name of MDF and LDF Files in the sp_attach_db sp. You have used three double quotes which will result in some syntax exception.
Try this:

cmd.CommandText = "sp_attach_db 'c:\databases\lms.mdf', 'c:\databases\lms.ldf'"

Happy coding...

Edited 7 Years Ago by gbpnkpm: n/a

This article has been dead for over six months. Start a new discussion instead.