NEWS

[C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL

[C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
Đăng bởi: Thảo Meo - Lượt xem: 3194 09:28:39, 23/02/2021C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn sử dụng thư viện MiniProfiler để xem log các câu lệnh thực hiện Sql server trên ứng dụng C#, Winform.

[C#] Using Mini Profiler get log excute SQL winform

Trong quản lý ứng dụng, nếu các bạn viết một ứng dụng lớn, và muốn xem người dùng khi chạy ứng dụng thực hiện những câu lệnh SQL gì.

Giúp cho người quản trị dễ dàng Debug và kiểm tra lỗi ứng dụng.

Ở bài viết này mình sẽ thực hiện lấy log từ thư viện Dapper.

Bạn nào chưa biết về Dapper có thể tìm hiểu về Dapper trong Blog của mình.

Giao diện Demo ứng dụng Get Log SQL C#:

mini_profile_logsql

Ở hình trên các bạn thấy, khi mình xử lý SQL từ Store Procedure hay Query bình thường, nó đều hiển thị ra ở TextBox cho mình.

Đầu tiên các bạn cài cho mình thư viện MiniProfiler.Integrations phiên bản hiện tại mình đang sử dụng là 2.5.1.

Lệnh cài đặt Nuget 

PM> Install-Package MiniProfiler.Integrations -Version 2.5.1

Các bạn cần chỉnh sửa lại file config cấu hình Database ở file SqlHelper.cs ở source code của mình bên dưới nhé.

Và ở mỗi câu lệnh muốn ghi log chúng ta sẽ sử dụng như sau:

public static DataTable ExecQueryDataAsDataTable(string T_SQL, object parametter = null)
{
    var profiler = CustomDbProfiler.Current;
    using (var connection = ProfiledDbConnectionFactory.New(new SqlServerDbConnectionFactory(configDatabase.ConnectString()), profiler))
    {
       
        connection.Open();
        var reader = connection.ExecuteReader(T_SQL, param: parametter, commandType: CommandType.Text);
        DataTable table = new DataTable();
        table.Load(reader);
        var commands = profiler.GetCommands();
        Console.WriteLine("Command SQL: " + commands);
        return table;
    }
}

Source code Form1.cs

namespace LogSqlMiniProfile
{
    public partial class Form1 : Form
    {
        TextWriter _writer = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var data = SQLHelper.ExecProcedureDataAsDataTable("pro_get_nhanvien", new { option = 2, manv = "007045"});


        }

        private void button2_Click(object sender, EventArgs e)
        {
            var data = SQLHelper.ExecQueryDataAsDataTable("select top 1 * from tbl_nhapphieu_dinhphoi");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _writer = new TextBoxStreamWriter(txtLog);
            Console.SetOut(_writer);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var data = SQLHelper.ExecQueryDataAsDataTable("select top 10 manv, tennv, diachi1, * from tbl_nhanvien");
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
Đăng bởi: Thảo Meo - Lượt xem: 3194 09:28:39, 23/02/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.