I am newbie to SQL Server Express. At the moment I am manually attaching the DBs using SSMS but I need to do it through code.
I have searched a lot over internet but could not really start anything.
I know there is a sp_attach procedure but donot know how to use it and then how to call that through my application’s setup. How to use SQLDMO
Could any help be given to me please?
Thanks
Didiit ever occur to you to READ THE DOCUMENTATION?
Sorry.
Lets see. Ever heard of that thing called “google”?
http://www.google.de/search?q=sp_attachdb+sql+server&sourceid=ie7&rls=com.microsoft:en-us:IE-Address&ie=&oe=&redir_esc=&ei=0EUuT9_IMc7J8gOHpKXtDg
“sp_attachdb sql server” has tons of links some to documentation, some to examples. You could invest more than half a second of one braincel before coming here and ask us to repro that.
Actually the stored procedure is officially retired. Anyhow, as you dont seem to be willing to use google yourself, here is a link to the online documentation for this stored procedure:
http://msdn.microsoft.com/en-us/library/ms179877.aspx
See, SQL Server is a commercial product. Everything is documented. This stored procedure is on the way out, the link to the documentation has the “new alternative”.
This is a hugh problem actually. First, you would call it like any other SQL statement. At the end, SQ LServer uses SQL which is text. The main problem is whether you should do that in the application set up or not, and how to do that within a setup. Different story – MSI files have limitations. if you are unexperienced enough to use Visual Studio setup proejcts instead of a real setup designer (read up on WIX) then you are way more limited on top.
Database creation in the setup is complicated unless you go very basic – you do NOT create a large high performance dtabase just automatically somewhere, disc layout is planned. That said, for small stuff it is ok 😉
At the end, you will at one point start a program setting up the database by connecting to the server and guess what – issuing SQL commands to set up the database. If you would use WIX (which is about the standard these days to make installers), then http://wix.mindcapers.com/wiki/WiX_and_SQLServer can help you getting started. The official WIX documentation also helps – I leave it to you as an exercise in using google to enter the proper terms into google.