Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup SQL Server via C#

How easy is it to backup a SQL Server database via C# code?

I see lots of related questions, but no real answers.

like image 837
Andrew Harry Avatar asked Mar 20 '09 03:03

Andrew Harry


People also ask

How do I backup a database in C#?

Now execute it using C# code. To do that create a Windows Forms application and drag and drop the following control onto the form. In the code above you can change the connection string corresponding to your database. Now run the application and select the server name and database name to create the backup.

What are the main 3 types of backups in SQL?

A backup of data in a complete database (a database backup), a partial database (a partial backup), or a set of data files or filegroups (a file backup).


2 Answers

Or: Generate your backup script in Management Studio, put it in a stored procedure, run procedure from C# code.

CREATE PROCEDURE sp_backup
AS
BEGIN
    BACKUP DATABASE [YourDatabase] TO  DISK = N'C:\YourPathAndFile.bak' 
    WITH NOFORMAT, NOINIT,  
    NAME = N'Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
END
GO
like image 113
cdonner Avatar answered Sep 18 '22 19:09

cdonner


SQL Management Objects - Microsoft.SqlServer.Management.Smo

It has the methods you need to complete that action.

like image 31
bioskope Avatar answered Sep 19 '22 19:09

bioskope