I have created my first project in C# which uses sql server database (file name: printing.mdf)
I have created a database on Sql Server Express and developed an application to manipulate that database.
After deploying (installing sql server on the client’s pc, then attaching database manually) everything works fine.
Real problem is that I want
- My application should install sql server automatically during installation.
- Database (printing.mfd) file should also be attached in installation process.
- MOST IMPORTANT: I want to save mdf file on D: drive, so that when system drive (C:) is formatted for any reason, my database should not lost.
For last problem I have tried this connection string
Data Source=.\SQLEXPRESS;AttachDbFileName=d:\printingData\printing.mdf;Integrated Security=True;User Instance=True
I also copied printing.mdf to d:\printingData
This worked on my pc but not on clients pc
Any suggestions specially related to 3rd one.
You can’t install SQL Server automatically. Even if this were possible, it would be highly UN-recommended as lots of system tests/configurations are done during the install.
You can attach database files to an existing SQL Server instance at install-time using a custom installation package. NSIS is the best freeware I have come across for installation and it is very well documented and supported (by the users etc.). The functionality you want for database attachment is one of the plugins found here on the NSIS Wiki pages
Of course the D: partition you may have on your PC may not exist on another user own machine. Also, there is not way of automatically creating such a partition on a user’s machine. To do, or attempt to do this would be very intrusive.
In my opinion, I would create some documentation specifying the recommended configuration for your software. You could also provide the installation instructions documenting how to partition the hard drive. But you will find that users (casual users esp.) will not want to be doing such things!
Edit: To answer your comment. For installation from the command prompt in see the following MSDN article – good luck, and be carefull attempting to automate this, it is NOT a good idea…
To attach a database using TSQL use the following queries:
Note you can use a system stored proceedure to attach databases but this is being phased out by Microsoft.
Hope this helps.