با سلام
توسط دستور زیر از دیتابی بکاپ می گیرم.
var sql = "BACKUP DATABASE sample to DISK='" + path + "' with compression"; tre.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, sql, "save");
حالا می خوام یک progressbar هم نشان داده شود
لطفا راهنمایی کنید.
با تشکر
سلام از این قطعه کد برای بکاپ استفاده کن:
public void StartBackupDatabase(string backupName, string backupDescription, string backupFilePath) { Task.Factory.StartNew(() => { if (backupDescription.IsNullOrEmpty()) backupDescription = " "; _Exception = null; if (Thread.CurrentThread.Name.IsNullOrEmpty()) { Thread.CurrentThread.Name = "SQL Backup"; } using (SqlConnection con = new SqlConnection(this.MasterConnectionString)) { //The FireInfoMessageEventOnUserErrors property and InfoMessage event ensure that the C# code captures these messages during execution rather than only at the end. con.FireInfoMessageEventOnUserErrors = true; con.InfoMessage += OnInfoMessage; con.Open(); //The stats = 1 clause tells SQL Server to emit severity 0 messages at the specified percentage interval (in this case 1%). using (var cmd = new SqlCommand("BACKUP DATABASE @database TO DISK = @filename with FORMAT,description = @description, name = @name, stats = 1", con)) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@database", Database); cmd.Parameters.AddWithValue("@filename", backupFilePath); cmd.Parameters.AddWithValue("@description", backupDescription); cmd.Parameters.AddWithValue("@name", backupName); cmd.CommandTimeout = (int)TimeSpan.FromMinutes(10).TotalSeconds; cmd.ExecuteNonQuery(); isBackupCompleted = true; } con.Close(); con.InfoMessage -= OnInfoMessage; con.FireInfoMessageEventOnUserErrors = false; } }).ContinueWith(task => { if (task.Exception != null) { OnCompleted(task.Exception); isBackupCompleted = false; } else if (!_Exception.IsNullOrEmpty()) { OnCompleted(new Exception(_Exception)); isBackupCompleted = false; } else { OnCompleted(null); isBackupCompleted = true; } }); }
private void OnInfoMessage(object sender, SqlInfoMessageEventArgs e) { foreach (SqlError info in e.Errors) { if (info.Class > 10) { _Exception += "\r\n" + info.Message; _Exception = _Exception.Trim(); // TODO: treat this as a genuine error } else { string numValue = info.Message.Split(" ".ToCharArray()).First(); int progressPercent; if (int.TryParse(numValue, out progressPercent)) { OnProgressChanged(progressPercent); } // TODO: treat this as a progress message } } }
private string _Exception; protected virtual void OnProgressChanged(int percent) { if (ProgressChanged != null) { ProgressChanged(this, new System.ComponentModel.ProgressChangedEventArgs(percent, null)); } } public event ActionCompletedEventHandler ActionCompleted; public event System.ComponentModel.ProgressChangedEventHandler ProgressChanged;
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)