Detatch, Copy and re-Atatch database

  • Thread starter Finn Stampe Mikkelsen
  • Start date
F

Finn Stampe Mikkelsen

Hi

I'm in need of a way to copy my local sql database from the running project
to make a backup.

My connectionstring is like this:

"Data
Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated
Security=True;Connect Timeout=60;User Instance=True"

I get the error, that the file is in use and cannot be copied. I figure i
need to detatch the file and then copy the files, but i can't seem to find
any example of the code needed.

Could anybody here show me the code needed to detatch the database end
subsequently re-atatch it, so my application can continue running and using
it. I need to both be able to backup and restore the files, but don't know
how..

TIA

/Finn
--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.
 
M

miher

Finn Stampe Mikkelsen said:
Hi

I'm in need of a way to copy my local sql database from the running
project to make a backup.

My connectionstring is like this:

"Data
Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated
Security=True;Connect Timeout=60;User Instance=True"

I get the error, that the file is in use and cannot be copied. I figure i
need to detatch the file and then copy the files, but i can't seem to find
any example of the code needed.

Could anybody here show me the code needed to detatch the database end
subsequently re-atatch it, so my application can continue running and
using it. I need to both be able to backup and restore the files, but
don't know how..

TIA

/Finn
--
Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
There are 10 kinds of people. Those who understand binary and those who
don't.
Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
nicht tuhen.

Hi,
Try using
http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.aspx .

Zsolt
 
Z

ZokiManas

You dont really have to do all the work, in terms of, detach, copy, attach.
As previously said, use SMO.

using Microsoft.SqlServer.Management.Smo;

string db = "AdventureWorks";
Backup bck = new Backup();
bck.Action = BackupActionType.Database;
bck.BackupSetName = db + "_BackupSet";
bck.Database = db;
bck.Devices.Add(new BackupDeviceItem(@"C:\" + db + ".bak", DeviceType.File));
bck.SqlBackup(new Server());

Hope it will help, or, give some ideas.
 
Z

ZokiManas

Additionally, if you need Restore function...

Restore myFullRestore = new Restore();
myFullRestore.Database = "DatabaseName";
myFullRestore.Action
=RestoreActionType.Database;myFullRestore.AddDevice(@"C:\BackupFile.bak",DeviceType.File);myFullRestore.FileNumber = 1;
myFullRestore.SqlRestore(myServer);

Might be handy :)
 
F

Finn Stampe Mikkelsen

ZokiManas said:
Additionally, if you need Restore function...

Restore myFullRestore = new Restore();
myFullRestore.Database = "DatabaseName";
myFullRestore.Action
=RestoreActionType.Database;myFullRestore.AddDevice(@"C:\BackupFile.bak",DeviceType.File);myFullRestore.FileNumber
= 1;
myFullRestore.SqlRestore(myServer);

Might be handy :)

Thank you very much. Actually i have solved it using SMO , but have not had
the time to write it here...

Thanks for your input anyway.. ;-))

/Finn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top