从我的 Access DB 的两个表中检索值并 subtracting/output C# 中的结果

Retrieving values from two tables from my Access DB and subtracting/output the result in C#

之前,我对 Visual Studio 和 C# 还很陌生,如果这个问题听起来有些愚蠢,我很抱歉。

所以我最近用一些 table 创建了一个 Access 数据库。 table其中一个是revenues(收入),一个是expenses。这两个 table 都有一个包含总收入 (income table) 和总支出 (expense table) 的列,所有内容都加在一起在这些列中。

可以在我用 C# 创建的前端 windows 表单中调整和查看这些值。

还可以查看收入和支出的2个总值。这是按如下方式完成的(在这个例子中留下了连接代码等):

try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select * from Income where Month='Januari'";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();   

            while (reader.Read())
            {

                total_income.Text = reader["Total income"].ToString();

            }                

            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }

try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query2 = "select * from Expenses where Month='Januari'";

            command.CommandText = query2;
            OleDbDataReader reader2 = command.ExecuteReader();  
            while (reader2.Read())
            {

                total_expenses.Text = reader2["Total expenses"].ToString();

            }

            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error" + ex);
        }

所以总收入和支出正在输出到一个标签中。

我想要实现的是在C#中减去总收入和总支出并将结果输出到标签或其他东西中(这样我就可以在表格中查看)。

我尝试了多种方法来实现它,例如我尝试存储 total_income.Text = reader["Total income"];进入一个 int 变量,与 total_expenses.Text 相同,但这不能在 try 块之外完成(因为我有 2 个支出和收入)这似乎是不可能的。还尝试使用 Convert.ToInt32(THE VAR) 做一些事情,但这似乎也不起作用..

有没有一种简单的方法可以从数据库中获取这 2 个不同的 table 值,然后在 C# 中用代码减去它们并将其输出到标签中?顺便说一句,这 2 个值在 Access 数据库中具有货币类型。

提前致谢!

您可以在 try/catch 块之前声明两个十进制变量,在块内使用它们并在块外进行数学计算

如果这两个字段不是整数,那么您应该使用适当的数据类型(十进制)来处理货币值,并使用适当的转换方法 (Convert.ToDecimal) 来设置这些局部变量

decimal incomeSum = 0m;
decimal expenseSum = 0m;
try
{
    .....
    while (reader.Read())
    {

        incomeSum = Convert.ToDecimal(reader["Total income"]);
        total_income.Text = income.ToString();
    }                

    ....
    while (reader2.Read())
    {
        expenseSum = Convert.ToDecimal(reader2["Total expenses"]);
        total_expenses.Text = expenseSum.ToString();
    }
}
catch (Exception ex)
{
    MessageBox.Show("Error" + ex);
}
balance.Text = (incomeSum - expenseSum).ToString();

顺便说一句,除非有未解释的原因,否则您不需要有两个单独的 try catch 块

如果我没理解错的话,我认为你的代码并没有按照你的意愿行事。迭代 reader 将用当前行覆盖 "total_" 标签。 Label 最终将显示结果集中最后一行的值。我认为您想对值求和,例如:

  string incomeQry = "SELECT SUM([Total income]) FROM [Income] WHERE [Month] = 'Januari';";
  decimal totalIncome = 0m;

  try
  {
    using (OleDbConnection cnxn = new OleDbConnection("connection string..."))
    {
      using (OleDbCommand cmd = new OleDbCommand(incomeQry, cnxn))
      {
        cmd.CommandType = System.Data.CommandType.Text;
        totalIncome = (Decimal)cmd.ExecuteScalar();
        total_income.Text = totalIncome.ToString();
      }
    }
  }
  catch (Exception)
  {        
    throw;
  }