I have created a project that uses classes to access some Tables
in SQL server. Meanwhile, I have created a "Setup and Deployment" project to
install this program on another computer by means of "Windows Installer".
But I need to "Attach" database files to SQL server on that computer manually in "Enterprise Manager".

Now I wonder if there is any way that I can "Attach" these Database files automaticlly, whithout any need to do this manually (For example is there any facility in "Windows Installer" for this purpose )

Re: ClickOnce and Setup & Deployment Projects "Windows Installer" and Attaching Database Files ?



I'm facing the same problem as you. My solution was to built a check into the startup of the application and check if it firstlyu can connect to the database if not check if the database exist usin SMO. if not then run the following script:

However you are assumeing a few thing here. The location of the logical file ( you could popup a openfiledailog requesting a path - better option)

Then using normal SQL command executennonquery run the command as an embedded resource or just build it up dynamically.

Not the best of solutions but it will work


-- Attach database template




FROM sys.databases

WHERE name = N'<database_name, sysname, your_database_name>'


CREATE DATABASE <database_name, sysname, your_database_name>

ON PRIMARY (FILENAME = '<database_primary_file_path,,C:\Program files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\your_database_name.MDF>')