4

My aim is to backup a database (.mdf) as one file with my web application project written in C#. The backup should later on be loaded on a "restore"-page, where the data in the backed-up tables could be appended to the original database row by row. What would be a good practice to implement this? I thought of just copying the mdf file, but then I read about attaching and detaching of the database. Furthermore I don't know what to do with the _log.ldf file.

I'm looking forward to your hints. Thank you in advance for your help!

EDIT: I can only use the free SQL Server Express for this, because I want to distribute my program to other people.

8
  • Do you have written the code for taking backup in C#? Commented Nov 16, 2011 at 11:11
  • I haven't written any code for backup, but the code behind of the asp.net/ web application project is written in C#. Commented Nov 16, 2011 at 11:13
  • Perhaps I do not understand, but if you backup the database and append the data again to the original database you get duplicate rows.... Commented Nov 16, 2011 at 11:19
  • By "rowwise" do you really need it to be rowwise, or are you just avoiding answers that say "copy and restore the DB files"? Commented Nov 16, 2011 at 11:22
  • @ Pleun: That is why I want to do the restore row by row. I mean that a user sees the entries of the backup-database and can select a whole row, if he wants it appended to the original-database. Commented Nov 16, 2011 at 11:23

4 Answers 4

4

Probably, you refer to the Backup and Restore using C# for Sql Server to get a complete idea about writting a code in C#, which has helped me a lot when I was using it.

By the use of Backup class in C#, you can get all the facilities to backup as well as restore.

Sign up to request clarification or add additional context in comments.

4 Comments

So I need MSSQL Server installed to use this library? Would MSSQL Server Express suffice?
No, need of installing MSSQL Server. You can work with .net default server explorer OLEDB connection.
Can you provide a solution to process the backup file and evaluate the restore on a row-by-row basis?
@skk: My program should be distributed to people with the free MSSQL Express. If I use an OLEDB connection I can not use the power auf the Backup class?
1

If you are only interested in appending the data rowwise afterwards, perhaps it is easier to export each table to CSV and import it afterwards (so you have rowwwise control in C#).

If you insist ine one file, just add all the CSV's to a zip.

You can use the FileHelpers library for this (http://www.filehelpers.com/) and you will have it up&running in no time.

Comments

0

Apparently there is a Backup class in the SQL Server Management Objects library.

You might want to check that out first, as it doesn't look overly complicated:

http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backup.aspx

It would require you to have two or three libraries installed on the server you run it on, though they are fairly small - not Windows SDK sized...

Comments

0

pls go through this link on how to backup data and restore using c# and sql server .In addition to you have to add these names spaces

Microsoft.SqlServer.Management.Smo;
Microsoft.SqlServer.Management.Common;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.