0

在我的 C# 应用程序中,我试图读取我的Accounts表中的数据,将数据读取为小数,对其执行 aa 计算,然后更新同一行。

现在它读取列中的正确数据,但是在尝试更新时出现了两件事。

它将AccountTotal列中的所有数据设置为相同的值。该值对于第一行是正确的,但对于其余行不正确。

我相信第二个问题出现在计算要更新的数据时。当我尝试更新数据库时,它会将值设置为我想要的两倍。例如:在我的CalculateIncome方法中,我不想将 100 添加到帐户总数中,而是添加 200。

是什么导致了这两个问题?

这是程序:

class Program
{
    static void Main(string[] args)
    {
        //Need to change when deploying on real database.
        const string DB_NAME = "Bank.sdf";
        const string DB_PATH = @"C:\Users\Lucas\eBankRepository\eBank\App_Data\" + DB_NAME; // Use ".\" for CWD or a specific path  
        const string CONNECTION_STRING = "Data Source=" + DB_PATH;

        decimal AccountTotal;

        var conn = new SqlCeConnection(CONNECTION_STRING);
        SqlCeDataReader reader = null;

        try
        {
            conn.Open();

            //Basic Query of all accounts
            SqlCeCommand Query = new SqlCeCommand("SELECT * FROM Accounts", conn);
            reader = Query.ExecuteReader();

            while (reader.Read())
            {
                AccountTotal = reader.GetDecimal(2); //Column in DB for Account Total
                AccountTotal += CalculateIncome();

                //Update Total
                SqlCeCommand UpdateTotal = new SqlCeCommand("UPDATE Accounts SET AccountTotal = @UpdatedTotal", conn); // Error when using WHERE Clause "WHERE AccountName= @ Savings"
                UpdateTotal.Parameters.AddWithValue("@UpdatedTotal", AccountTotal);
                UpdateTotal.Connection = conn;
                UpdateTotal.ExecuteNonQuery();
            }
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }

            if (conn != null)
            {
                conn.Close();
            }
        }
    }

    public static decimal CalculateIncome()
    {
        return 100;
    }
}

编辑:

这是我之前在命令中包含 WHERE 子句的代码。使用此代码,它现在仅更新其帐户名称为“Savings”的行,但仍将每行中的值设置为与AccountTotal相同

            while (reader.Read())
            {
                AccountTotal = reader.GetDecimal(2); //Column in DB for Account Total
                AccountTotal += CalculateIncome();

                //Update Total
                SqlCeCommand UpdateTotal = new SqlCeCommand("UPDATE Accounts SET AccountTotal = @UpdatedTotal WHERE AccountName= @Savings", conn); // Error when using WHERE Clause "WHERE AccountName= @ avings"
                UpdateTotal.Parameters.AddWithValue("@UpdatedTotal", AccountTotal);
                UpdateTotal.Parameters.AddWithValue("@Savings", "Savings");
                UpdateTotal.Connection = conn;
                UpdateTotal.ExecuteNonQuery();
            }

这是程序运行前后的视觉效果。

前

后

工作代码

            while (reader.Read())
            {
                AccountTotal = reader.GetDecimal(2); //Column in DB for Account Total
                //Console.WriteLine(AccountTotal);
                AccountTotal += CalculateIncome();
                //Console.WriteLine(AccountTotal);

                //Update Total
                SqlCeCommand UpdateTotal = new SqlCeCommand("UPDATE Accounts SET AccountTotal = @UpdatedTotal WHERE AccountName = @Savings AND AccountID = @ID", conn);
                UpdateTotal.Parameters.AddWithValue("@UpdatedTotal", AccountTotal);
                UpdateTotal.Parameters.AddWithValue("@Savings", "Savings");
                UpdateTotal.Parameters.AddWithValue("@ID", reader.GetInt32(0));
                UpdateTotal.Connection = conn;
                UpdateTotal.ExecuteNonQuery();

                AccountTotal = 0; //Reset
            }
4

1 回答 1

4

你的两个问题是:

  1. 它将所有行更新为相同的值这是因为更新语句中没有 where 子句。

  2. 它使价值翻了一番。
    这是因为这行AccountTotal += CalculateIncome(); What this does is in the first run make it be 100 and the second loop around it is 200.

于 2013-07-15T23:38:20.560 回答