我正在C#中做項目。我在我的項目中使用SQL數據庫。我想投入項目數據庫備份和恢復功能。我必須做什麼/爲它編碼?添加數據庫備份和恢復功能?
0
A
回答
0
是要備份整個數據庫還是隻想恢復到更改數據庫之前的狀態?
第一種方法在程序中很少見,畢竟:在備份時確定沒有其他人正在更改數據庫嗎?
後者很常見:在對數據庫進行若干更改之前,請記住更改之前的狀態,進行更改並決定是否要在更改之前保留更改或返回到該狀態,例如由於錯誤。
後者是使用System.Data.SqlClient.SqlTransaction類完成的。
using (var sqlConnection = GetSqlConnection(...))
{
try (sqlConnection.Open()
{ // before making the changes: remember the state of the database
using (var transaction = sqlConnection.BeginTransaction())
{
bool changesOk = true;
try
{
changesOk = ChangeDatabase(...);
}
catch (Exception exc)
{
changesOk = false;
}
if (!changesOk)
{ // go back to the state at the beginning of the transaction
transaction.RollBack();
}
else
{ // no error: save the changes:
transaction.Commit();
}
}
}
}
+0
我想備份完整的數據庫並進行恢復。 –
0
**Backup and Restore SQL Server databases programmatically with SMO **
SMO提供了實用工具類爲特定的任務。對於備份和還原,它提供了Microsoft.SqlServer.Management.Smo命名空間中可用的兩個主要實用程序類(備份和還原)。
這是示例代碼。
Backup bkpDBFull = new Backup();
/* Specify whether you want to back up database or files or log */
bkpDBFull.Action = BackupActionType.Database;
/* Specify the name of the database to back up */
bkpDBFull.Database = myDatabase.Name;
/* You can take backup on several media type (disk or tape), here I am
* using File type and storing backup on the file system */
bkpDBFull.Devices.AddDevice(@"D:\AdventureWorksFull.bak", DeviceType.File);
bkpDBFull.BackupSetName = "Adventureworks database Backup";
bkpDBFull.BackupSetDescription = "Adventureworks database - Full Backup";
/* You can specify the expiration date for your backup data
* after that date backup data would not be relevant */
bkpDBFull.ExpirationDate = DateTime.Today.AddDays(10);
/* You can specify Initialize = false (default) to create a new
* backup set which will be appended as last backup set on the media. You
* can specify Initialize = true to make the backup as first set on the
* medium and to overwrite any other existing backup sets if the all the
* backup sets have expired and specified backup set name matches with
* the name on the medium */
bkpDBFull.Initialize = false;
/* Wiring up events for progress monitoring */
bkpDBFull.PercentComplete += CompletionStatusInPercent;
bkpDBFull.Complete += Backup_Completed;
/* SqlBackup method starts to take back up
* You can also use SqlBackupAsync method to perform the backup
* operation asynchronously */
bkpDBFull.SqlBackup(myServer);
private static void CompletionStatusInPercent(object sender, PercentCompleteEventArgs args)
{
Console.Clear();
Console.WriteLine("Percent completed: {0}%.", args.Percent);
}
private static void Backup_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Backup completed.");
Console.WriteLine(args.Error.Message);
}
private static void Restore_Completed(object sender, ServerMessageEventArgs args)
{
Console.WriteLine("Hurray...Restore completed.");
Console.WriteLine(args.Error.Message);
}
相關問題
- 1. 備份和恢復mysql數據庫c#
- 2. 備份和恢復realm.io數據庫
- 3. 備份和恢復數據庫android studio
- 4. 備份和恢復數據庫
- 5. java數據庫備份和恢復
- 6. 數據庫備份和恢復
- 7. PostgreSQL:數據庫備份和恢復?
- 8. 備份和恢復MS SQL數據庫
- 9. 備份和恢復SQLCE .sdf數據庫
- 10. LocalDB-主數據庫,備份/恢復功能
- 11. 我怎麼能從mysql數據庫備份和恢復日期
- 12. 恢復數據庫備份問題
- 13. ObjectBox - 數據庫備份/恢復
- 14. Yii備份/恢復MSSQL數據庫
- 15. 恢復數據庫備份的錯誤
- 16. 恢復WordPress數據庫但不備份
- 17. 錯誤恢復postgres數據庫備份
- 18. Android的數據庫備份恢復
- 19. PostgresSQL數據庫從備份恢復
- 20. 數據庫備份/恢復過程
- 21. 無備份恢復數據庫
- 22. 從備份恢復數據庫
- 23. 恢復java derby數據庫備份
- 24. 備份和恢復加密數據庫(sqlcipher,cacheword)?
- 25. Kinvey數據備份和恢復
- 26. Solrcloud備份和恢復索引數據
- 27. SQL Server Management Studio:無數據備份和恢復數據庫
- 28. Android備份/恢復:如何備份內部數據庫?
- 29. mysql數據庫備份類 - 幫我添加PORT功能
- 30. Micro Services中的數據複製:恢復數據庫備份
http://www.c-sharpcorner.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/ – Nalaka
其中的RDBMS用的?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –
我正在使用sql server 2008 –