I have C#.Net winform application having SQL Server Database in SQLExpress with Windows Authentication.
There is a Option related to Backup and restore batabase. In DBbackup i have provided user to select folder and database will be copy to that folder.
for restore database i have written following line:
System.IO.File.Copy(filePath, Application.StartupPath + "\\dbSTK.mdf",true);
it throws an exception saying dbSTK is already in use. How can i overwrite file that is already in use?
A .mdf file is not a backup file, but the actual data file. You cannot simply copy a running database (or overwrite a running database.) Typically you would have to use
sp_detach_dbto get the file in state that you can copy/archive andsp_attach_db(or create database for attach) to restore the db.If you just made a copy of the running .mdf as a backup, then the odds are pretty good that you won’t be able to attach that file.
While the above is the correct method to detach/attach a database, I would suggest actually performing a backup/restore. It doesn’t require taking the database offline and is generally a better approach to managing backups.
How to attach a data file in SQL Server: http://msdn.microsoft.com/en-us/library/ms179877.aspx
How to detach a database in SQL Server: http://msdn.microsoft.com/en-us/library/ms188031.aspx
How to perform a backup on SQLEXpress: http://msdn.microsoft.com/en-us/ms186865.aspx
How to restore a backup on SQLExpress: http://msdn.microsoft.com/en-us/ms186858