How to back up a SQL Server database (mdf) programmatically so you can import it later?

My goal is to backup the database (.mdf) as a single file with my web application project written in C #. Then the backup should be uploaded to the β€œrestore” page, where the data in the backup tables can be added to the source database in a row. What would be good practice to implement this? I thought that I was just copying the mdf file, but then I read about attaching and detaching the database. In addition, I do not know what to do with the _log.ldf file.

I look 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.

+4
source share
4 answers

You are probably referring to Backup and Restore using C # for Sql Server to get a complete picture of writing C # code, which has helped me when I used it.

Using the Backup class in C #, you can get all the features for backup and recovery.

+4
source

If you are only interested in adding data after childbirth, it may be easier to export each table to CSV and import it afterwards (so you have a foray into C #).

If you insist on a single file, just add all the CSVs to zip.

You can use the FileHelpers library for this (http://www.filehelpers.com/), and you can quickly and quickly launch it.

+1
source

There seems to be a Backup class in the SQL Server Management Object Library.

You can check this out first, as it doesn't look overly complicated:

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

This will require that two or three libraries be installed on the server on which you ran it, although they are quite small - not the size of the Windows SDK ...

0
source

Pls follow this link on how data backup and restore using c # and sql server. In addition, you need to add these namespaces.

 Microsoft.SqlServer.Management.Smo; Microsoft.SqlServer.Management.Common; 
0
source

Source: https://habr.com/ru/post/1381467/


All Articles