1

我面临一个问题,每次刷新页面,支付的贷款都会增加 500。我理解我的逻辑是错误的,从申请贷款之日起 1 个月后,“loanPaid”将增加 500,但我希望发生的是每个下个月它将增加500美元。如果有人可以帮助我理解逻辑。我会很感激。我正在考虑使用一些循环,但不确定是哪一个以及如何使用。我只是一名大一学生,所以请原谅我的编码风格。谢谢


public class LoanDAL
{
    string connString = ConfigurationManager.ConnectionStrings["Oakhorizons"].ToString();
    public LoanDAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataTable getAllLoanInfoDT()
    {
        using (SqlConnection conn = new SqlConnection(connString))
        {
            DataTable dt = new DataTable();
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = conn;
            // cmd.CommandType = CommandType.StoredProcedure;
            cmd2.CommandText = "SELECT DISTINCT purchaseDate FROM LoanPortfolio WHERE (custID LIKE 'OH00002') AND (loanType LIKE 'Personal Loan')";
            cmd2.Parameters.AddWithValue("@custID", "OH00002");
            cmd2.Parameters.AddWithValue("@loanType", "Personal Loan");
            conn.Open();
            string custID = "OH00002";
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd2;
            da.Fill(dt);
            int iMonthNo = int.Parse(System.DateTime.Now.Month.ToString());
            DateTime dtDate = new DateTime(2000, iMonthNo, 1);
            double dMonthNow = Double.Parse(dtDate.ToString("MM"));
            LoanTableAdapters.LoanPortfolioTableAdapter loanAdapter = new LoanPortfolioTableAdapter();
            string LoanDate = loanAdapter.RetrieveData(custID.ToString()).ToString();
            string month = dt.ToString();

            double dLoanDate = Double.Parse(LoanDate.Substring(3, 2));

            if (dMonthNow > dLoanDate)
            {
                String sql = "UPDATE LoanPortfolio SET loanPaid = loanPaid + 500";
                sql += "WHERE (loanType LIKE 'Personal Loan') AND (custID LIKE 'OH00002')";
                cmd2.Connection = conn;
                cmd2.CommandText = sql;

                cmd2.ExecuteNonQuery();

            }
            conn.Close();
}
}

编辑后:

 public DataTable getAllLoanInfoDT()
{ 
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd2 = new SqlCommand();
        cmd2.Connection = conn;
        // cmd.CommandType = CommandType.StoredProcedure;
        cmd2.CommandText = "SELECT DISTINCT loanUpdateDate FROM LoanPortfolio WHERE (custID LIKE 'OH00002') AND (loanType LIKE 'Personal Loan')"; 
        cmd2.Parameters.AddWithValue("@custID", "OH00002");
        cmd2.Parameters.AddWithValue("@loanType", "Personal Loan");
        conn.Open();
        SqlDataReader myReader = cmd2.ExecuteReader();
        DateTime loanUpdateDate = Convert.ToDateTime(myReader);
        DateTime currDateTime = DateTime.Now;

        int loanToBeAdded = (((currDateTime.Year - loanUpdateDate.Year) * 12) + currDateTime.Month - loanUpdateDate.Month) * 500;
        if (loanToBeAdded > 0)
        {
            String sql = "UPDATE LoanPortfolio SET loanPaid = loanPaid + " + loanToBeAdded.ToString() + ", loanUpdateDate = " + DateTime.Now.ToString();
            sql += " WHERE (loanType LIKE 'Personal Loan') AND (custID LIKE 'OH00002')";

            //Execute the above query here
        }
        conn.Close();

        using (SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM LoanPortfolio where custID like 'OH00002'", conn))
        {
            DataTable dTable = new DataTable();
            dAd.Fill(dTable);
            return dTable;
        }
    }
}
4

3 回答 3

0

You should also indicate in your table when you increased the loan for the last time and check this value, too. You could insert a new column lastLoanIncrease in the table LoanPortFolio as char(6) and save the month and year, when you increased it. Then before increasing again check for it.

于 2013-07-29T09:21:17.770 回答
0

你做了很多不必要的事情,一些重要的检查丢失了,你的代码中也出现了一些抑制性能的错误,但我不会在这里指出它们,因为你是大一新生,你会逐渐学习。

您的问题的解决方案应该如下

  1. 首先在“LoanPortfolio”表中创建一个新列,即“LastUpdatedLoanPaidDate”。此列的类型应为日期。这将存储您上次将 500 美元添加到“loanPaid”列的日期。

  2. 当您第一次在“LoanPortfolio”表中添加一行时,将此列设置为与“purchaseDate”相同。所以最初“purchaseDate”和“LastUpdatedLoanPaidDate”将是相同的。

  3. 仅获取“LastUpdatedLoanPaidDate”而不是“purchaseDate”作为

        cmd2.CommandText = "SELECT DISTINCT LastUpdatedLoanPaidDate FROM LoanPortfolio WHERE (custID LIKE 'OH00002') AND (loanType LIKE 'Personal Loan')";
    
  4. 假设从上述查询中仅获取 1 条记录,以下代码应每月向“loadPaid”列添加 500 美元

        //Fetch "LastUpdatedLoanPaidDate" here
    
        //This will be "LastUpdatedLoanPaidDate" coming from database i.e. dt[0][0].ToString(). Hard-coded here for simplicity
        string strLastUpdatedLoanPaidDate = "07/29/2013";
    
        DateTime lastUpdatedLoanPaidDate = Convert.ToDateTime(strLastUpdatedLoanPaidDate);
        DateTime currDateTime = DateTime.Now;
    
        //This will make sure that all the previous purchases are also handled and not just previous month's
        //This is important when you are implementing new logic on existing data
        int loanToBeAdded = (((currDateTime.Year - lastUpdatedLoanPaidDate.Year) * 12) + currDateTime.Month - lastUpdatedLoanPaidDate.Month) * 500;
    
        //If loadToBeAdded is zero then need not update database
        if (loanToBeAdded > 0)
        {
            String sql = "UPDATE LoanPortfolio SET loanPaid = loanPaid + " + loanToBeAdded.ToString() + ", LastUpdatedLoanPaidDate = " + DateTime.Now.ToString();
            sql += " WHERE (loanType LIKE 'Personal Loan') AND (custID LIKE 'OH00002')";
    
            //Execute the above query here
        }
    

根据您的要求,我可能会缺少一些东西。此外,更新语句可能需要调整,因为我尚未对其进行测试,但总的来说这应该可以解决问题。

希望这有帮助。

问候,

萨马尔

于 2013-07-29T11:41:02.357 回答
0

你不需要循环。

你的问题是在你的条件if

//DateTime loanDate = new DateTime(2000, 1, 18);
DateTime loanDueDate = Foo.GetLoanDueDate(loanId);

int loanDueMonth = loanDueDate.Month;
int currentMonth = DateTime.Now.Month;

if (currentMonth > loanDueMonth)
    {
        // update db
        loanDate.AddMonths(1);
        Foo.UpdateLoanDueDate(loanId, loanDate); // increase loan due date for next month so that the conditional is true next month.
    }
于 2013-07-29T09:23:34.120 回答