- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
[C#] Hướng dẫn Restore Database Sqlserver with Progress bar sử dụng thư viện SQL SMO
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn sử dụng thư viện Sql SMO của Microsoft cung cấp để thực hiện khôi phục dữ liệu Restore database Sqlserver và hiển thị phần trăm hoàn thành lên Progressbar C#.
Trong lập trình ứng dụng C#, các ứng dụng quản lý, chúng ta thường viết thêm một module để thực hiện công việc backup và restore database hàng ngày.
Và trong bài viết này, các bạn có thể sử dụng thể thực hiện công việc khôi phục dữ liệu cho ứng dụng của mình.
Giao diện demo ứng dụng Restore Database C#:
Đầu tiên, các bạn cần import thư viện sqlserver sql smo vào, các bạn có thể download ứng dụng của mình bên dưới cuối bài để import thư viện vào nhé.
Source code C# Restore Database Sqlserver:
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RestoreDatabaseSQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_restore_Click(object sender, EventArgs e)
{
progressBar.Value = 0;
try
{
Server dbServer = new Server(new ServerConnection(txt_server.Text, txt_username.Text, txt_password.Text));
Restore dbRestore = new Restore() { Database = txt_database.Text, Action = RestoreActionType.Database, ReplaceDatabase = true, NoRecovery = false };
dbRestore.Devices.AddDevice(txt_filebackup.Text, DeviceType.File);
dbRestore.PercentComplete += DbRestore_PercentComplete;
dbRestore.Complete += DbRestore_Complete;
dbRestore.SqlRestoreAsync(dbServer);
}
catch (Exception ex)
{
lbl_status.Text = ex.Message;
}
}
private void DbRestore_Complete(object sender, ServerMessageEventArgs e)
{
if (e.Error != null)
{
lbl_percent.BeginInvoke(new Action(() =>
{
lbl_percent.Text = e.Error.Message;
}));
}
}
private void DbRestore_PercentComplete(object sender, PercentCompleteEventArgs e)
{
progressBar.BeginInvoke(new Action(() =>
{
progressBar.Value = e.Percent;
progressBar.Update();
}));
lbl_percent.BeginInvoke(new Action(() =>
{
lbl_percent.Text = $"{e.Percent}%";
}));
}
}
}
HAPPY CODING
Tham khảo bài viết từ youtube FoxLearn