How to backup Sql Database Programmatically in C#

后端 未结 10 1871
说谎
说谎 2020-12-23 11:48

I want to write a code to backup my Sql Server 2008 Database using C# in .Net 4 FrameWork. Can anyone help in this.

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 12:10

    You can use the following queries to Backup and Restore, you must change the path for your backup

    Database name=[data]

    Backup:

    BACKUP DATABASE [data] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\data.bak' WITH NOFORMAT, NOINIT,  NAME = N'data-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO
    

    Restore:

    RESTORE DATABASE [data] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\data.bak' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10
    GO
    

提交回复
热议问题