Marc
Marc

Reputation: 2859

Backup SQL Server database in ASP.NET

I want to backup a SQL Server database with C#. I have written a stored procedure which backup the database:

CREATE PROCEDURE WINC_BackupDatabase
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    BACKUP DATABASE WINC_kentico
    TO DISK = 'G:\MSSQL10.MSSQLSERVER\MSSQL\Backup\WINC_kentico_' + NOW() + '.bak'
    WITH NOINIT
END
GO

In line 10 I set the path for the backup. I want to add the current DateTime at the end of the file name. How is the syntax to set a DataTime at the end of the file name?

Best regards

Upvotes: 1

Views: 3521

Answers (1)

LukLed
LukLed

Reputation: 31882

DECLARE @NAME VARCHAR(250);
SET @NAME = 'G:\MSSQL10.MSSQLSERVER\MSSQL\Backup\WINC_kentico_' + CONVERT(VARCHAR,GETDATE(),112) + '.bak';
BACKUP DATABASE WINC_kentico
TO DISK = @NAME
WITH NOINIT

Upvotes: 7

Related Questions