如何在特定日期运行方法,也只在.net中运行一次

Tom*_*007 0 c#

我正在C#.net windows应用程序中创建一个银行管理系统应用程序.与所有银行一样,账户持有人(储蓄银行账户)在月底支付利息(例如当前余额的3.5%).我为它准备了代码.

    public void frmbankmg_Load(object sender, EventArgs e)

    {
        try
        {
            Transact();

        DateTime date = DateTime.Now.Date;
        //DateTime date = DateTime.Parse("Nov 1,2010");
        int day = date.Day;
        double Account = 0, Balance = 0;
        string status = "";
        if (day == 1)
        {
            SqlConnection conn = ConnectionString();
            string s = "select Account_No,Balance_Amount,Status from SavingAcct";
            SqlCommand cmd = new SqlCommand(s, conn);
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                Account = Convert.ToDouble(rd["Account_No"].ToString());
                Balance = Convert.ToDouble(rd["Balance_Amount"].ToString());
                status = rd["Status"].ToString();
                if (status == "Open")
                {
                    Interest(Account, Balance);
                }
            }
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}        

private void Transact()
{
    try
    {
        DateTime date = DateTime.Now.Date;
        int month = date.Month;
        int year = date.Year;
        int days = DateTime.DaysInMonth(year, month);
        if (days == date.Day)
        {
            double Account = 0, Balance = 0;
            string status = "";

            SqlConnection conn = ConnectionString();
            string s = "select Account_No,Balance_Amount,Status from SavingAcct";
            SqlCommand cmd = new SqlCommand(s, conn);
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                Account = Convert.ToDouble(rd["Account_No"].ToString());
                Balance = Convert.ToDouble(rd["Balance_Amount"].ToString());
                status = rd["Status"].ToString();
                if (status == "Open")
                {
                    string Transaction = "No";
                    string s1 = "insert into Transactions(Account_No,Date,Current_Balance,Month,Year,Transact) values('" + Account + "','" + date + "','" + Balance + "','" + month + "','" + year + "','" + Transaction + "'";
                    SqlCommand cmd1 = new SqlCommand(s1, conn);
                    conn.Open();
                    cmd1.ExecuteNonQuery();
                    conn.Close();
                }
            }
            conn.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void Interest(double Acctno,double balance)
{
    try
    {
        DateTime date = DateTime.Now.Date;
        //DateTime date = DateTime.Parse("Nov 1,2010");
        int month = date.Month;
        int year = date.Year;
        month = month - 1;
        SqlConnection conn = ConnectionString();
        string s = "select Date,Current_Balance from Transactions where Account_No='" + Acctno + "' and Month='" + month + "'";
        SqlDataAdapter da = new SqlDataAdapter(s, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Transactions");
        int totalRows = ds.Tables["Transactions"].Rows.Count;
        conn.Open();
        DateTime date1 = DateTime.Now, date2, date3;
        int row, row1 = -1, row2 = -1, day1, day2;
        for (row = 0; row < totalRows; row++)
        {
            date1 = (DateTime)ds.Tables["Transactions"].Rows[row][0];
            int d = date1.Day;
            if (d <= 10)
            {
                date2 = date1;
                day1 = d;
                row1 = row;
            }
            else
            {
                date3 = date1;
                day2 = d;
                row2 = row;
            }
        }
        double Bal1 = 0;
        if (row1 == -1)
        {
            Bal1 = NoTransactions_Bet1_and_10(Acctno, month);
        }
        else
        {
            Bal1 = Convert.ToDouble(ds.Tables["Transactions"].Rows[row1][1]);
        }
        double Bal2 = Convert.ToDouble(ds.Tables["Transactions"].Rows[row2][1]);
        double credit = 0;
        if (Bal1 <= Bal2)
        {
            credit = (3.5) / 100 * Bal1;
            balance = balance + (credit);
        }
        else
        {
            credit = (3.5) / 100 * Bal2;
            balance = balance + (credit);
        }
        string str = "update SavingAcct set Balance_Amount='" + balance + "' where Account_No='" + Acctno + "'";
        SqlCommand cmd = new SqlCommand(str, conn);
        cmd.ExecuteNonQuery();


        string comment = "By Interest";
        string transact = "Yes";
        string str1 = "insert into Transactions(Account_No,Date,Particulars,Credit,Current_Balance,Month,Year,Transact) values('" + Acctno + "','" + DateTime.Now.Date + "','" + comment + "','" + credit + "','" + balance + "','" + month + "','" + year + "','" + transact + "')";
        SqlCommand cmd1 = new SqlCommand(str1, conn);
        cmd1.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private double NoTransactions_Bet1_and_10(double Acct_no,int month)
{
    try
    {
        month = month - 1;
        string s = "select * from Transactions where Account_No='" + Acct_no + "' and Month='" + month + "'";
        SqlConnection con = ConnectionString();

        SqlDataAdapter da = new SqlDataAdapter(s, con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Transactions");

        con.Open();
        int Tot_row = ds.Tables["Transactions"].Rows.Count;
        double Balance = Convert.ToDouble(ds.Tables["Transactions"].Rows[Tot_row - 1][6]);
        con.Close();

        return Balance;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在表单加载事件中执行interest()方法时,此方法将在应用程序启动的月份的第一天运行.但每次关闭并重新启动我的应用程序时,它也会在同一天运行.因此,每当申请在每月的第1天开始时,将支付利息.由于没有特定的时间启动应用程序,所以我无法使用时间逻辑.你能帮我解决这个问题吗?

Mar*_*cie 5

您没有任何保证,任何人都将运行应用程序,更别说他们将运行它.所以这样的逻辑应该在应用程序外部处理,在Windows服务中,甚至是数据库作业,您可以依赖它在特定时间间隔或某个特定时间运行.