c# - Windows 窗体程序在运行查询时突然关闭。如何在错误停止/崩溃之前捕获错误?

标签 c# mysql database winforms crash

我有一个 3 PC 设置,运行我创建的类似 POS 的程序。 1 台 PC 作为服务器,2 台作为客户端。我在客户端有一个很重的查询,当执行时,有时会关闭程序。如何在强制关闭之前发现问题?

查询通过获取所有输入库存的总和并从已售出的产品、转移到另一个分支机构和损坏的产品中减去它来检查剩余库存。因此,每次他们搜索产品时,都会通过 4 个表(Inventory、Sales、Transferred、Damaged)获取库存,并通过另外 4 个表获取产品描述(Product、Category、Subcategory、Supplier)。

那么回过头来,我如何在应用程序关闭之前记录错误?我正在考虑获取表单关闭事件,但如何仅在它崩溃时记录它?

编辑 1:我总是将带有消息框的 try catch 放入我的所有方法中。该应用程序在关闭前不会弹出任何消息。

SELECT p.Id,p.Product_Name Product,p.Description,
c.Category_Name Category,sc.Subcategory_Name Subcategory,s.Supplier_Name Supplier,p.Selling_Price `Unit Price`,
i.Stocks,s.Sales,i.Stocks - IFNULL(s.Sales, 0) - IFNULL(t.Transfer, 0) - IFNULL(d.Damage, 0) AS Remaining

FROM (SELECT Id, Product_Name, Description, Selling_Price, Category_Id, Subcategory_Id, Supplier_Id FROM product WHERE enable_flag = 1) p

LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(quantity), 0) AS Stocks FROM inventory WHERE enable_flag = 1 GROUP BY product_id) i
ON p.Id = i.product_id

LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(quantity), 0) AS Sales FROM sales_detail WHERE enable_flag = 1 GROUP BY product_id) s
USING(product_id)

LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(transfer_quantity), 0) AS Transfer FROM stock_transfer WHERE enable_flag = 1 GROUP BY product_id) t
USING(product_id)

LEFT OUTER JOIN(SELECT product_id, COALESCE(SUM(damaged_quantity), 0) AS Damage FROM damaged_product WHERE enable_flag = 1 GROUP BY product_id) d
USING(product_id)

JOIN Category c ON p.Category_Id=c.Id
JOIN Subcategory sc ON p.Subcategory_Id=sc.Id
JOIN Supplier s ON p.Supplier_Id=s.Id;

编辑 2:

这是我的按钮代码(Variables.dgvSearchItemsDataSource 相当于发布的查询):

private void btnSearch_Click(object sender, EventArgs e)
{
    dgvSearchItems.DataSource = dbConnect.DatabaseToDatagrid(Variables.dgvSearchItemsDataSource + " WHERE p.Product_Name LIKE '" + cmbSrchProd.Text + "%'");
    if (dgvSearchItems.Rows.Count != 0)
    {
        this.dgvSearchItems.Columns[1].Frozen = true;
        this.dgvSearchItems.Columns[2].Frozen = true;
        this.dgvSearchItems.Columns[0].Visible = false;
        this.dgvSearchItems.Columns[7].Visible = false;
        this.dgvSearchItems.Columns[8].Visible = false;
    }
    txtQuantity.Focus();
}

我用来连接到我的数据库的方法:

private bool OpenConnection()
{
    try
    {
        connection.Open();
        return true;
    }
    catch (MySqlException ex)
    {
        switch (ex.Number)
        {
            case 0:
                myNotification = new frmNotifOk();
                myNotification.Show("Cannot connect to server.");
                break;

            case 1045:
                myNotification = new frmNotifOk();
                myNotification.Show("Invalid username/password, please try again.");
                break;
        }
        return false;
    }
}

最佳答案

看起来虽然您认为您在每个调用周围放置了 try/catch block ,但在某个地方抛出了未处理的异常。

我认为 https:System.AppDomain.UnhandledException可能有帮助。

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. If sufficient information about the state of the application is available, other actions may be undertaken — such as saving program data for later recovery

处理此事件将使您有机会在程序关闭之前记录问题。

用法:

static class Program
{
    private static MyLogger logger = new MyLogger();

    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += UnhandledException;

        ...
    }

    static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var exc = e.ExceptionObject as Exception;
        if (exc != null)
        {   // log the exception
            logger.LogException(exc);
        }
        // Problem is logged, Can't continue, so quit the application:
        Environment.Exit(-1);
    }

关于c# - Windows 窗体程序在运行查询时突然关闭。如何在错误停止/崩溃之前捕获错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969827/

相关文章:

mysql - SQL查找同一列中增量值的最大值

c# - 使用 Microsoft.Net.Http 将文件发送到服务

javascript - 如何在对象中显示数据

PHP Curly Quote 字符编码问题

sql - 我如何连接到 ODBC Oracle 数据库?

数据库字符限制代码点火器

c# - 如何控制 DateTimePicker 中的时间间隔

c# - Index-1 没有值

c# - 应用栏窗口从停靠位置弹出,然后移动到停靠位置

php - 循环内的 SUM - MYSQL