How easy is it to backup a SQL Server database via C# code?
I see lots of related questions, but no real answers.
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.
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).
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
SQL Management Objects - Microsoft.SqlServer.Management.Smo
It has the methods you need to complete that action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With