想請問asp.net C#的差勤系統,要先從sql撈差勤資料,並寫到另一張table,但要每五個特定時間點就執行去撈資料出來並寫入到另一張table,這要怎麼寫法會比較容易一點?
舉例,我要五個時間點09、12、15、18、21排程執行程式,先從A表撈到差勤資料,寫入B表裡面,這種要怎麼用判斷跟迴圈方式去寫出來?
謝謝各位的指教。
補上code來討論,要修改的話,要怎麼修改才會是我想要的結果??
if (_Time.Contains(DateTime.Now.ToString("HH")))
        {            
            logger.Info("OnTimer_UpdateDateTime Start");
            DBManager dbERP = null;
            DBManager dbHR = null;
            IDbTransaction transHR = null;
            DateTime baseDD = DateTime.Now;
            List<DoorLog> listModySource = new List<DoorLog>();
            List<DoorLog> listAddSource = new List<DoorLog>();
            try
                {
                    // 取得最後的更新異動日期
                    string strLastDD = Utility.ReadSetting("LAST_DateTime");
                    // 取得最後的更新系統日期
                    string strLastSysDD = Utility.ReadSetting("LAST_DateTime_SYS");
                    // 取得ERP端資料庫連線
                    dbERP = Utility.CreateDBManager("ERP");
                    // 建立HR端資料庫連線
                    dbHR = Utility.CreateDBManager("HR");
                    transHR = dbHR.BeginTransaction();
                    if (listModySource != null && listModySource.Count > 0)
                    {
                        List<DoorLog> listInSource = listModySource.Where(w => _DateTime.Contains(w.UserID) && _DateTime.Contains(w.CardNo))
                          .Distinct().ToList();
                        logger.Info("OnTimer_UpdateDateTime listInSource : " + listInSource.Count);
                        // In
                        foreach (var varSrc in listInSource)
                        {
                            // 先判斷WMS是否有存在
                            List<DoorLog> listTime = DoorLogRepository.Select(dbHR, transHR, varSrc.CardNo, varSrc.EmployeeID);
                            if (listTime != null && listTime.Count > 0)
                            {
                                // 取得最後一筆
                                DoorLog objTime = listTime.OrderByDescending(w => w.DateTime).FirstOrDefault();
                            }
                            else // 當不存在時,則寫入一筆資料進WMS
                            {
                                varSrc.DateTime = DateTime.Now;
                                DoorLogRepository.Insert(dbHR, transHR, varSrc);
                                logger.Info("OnTimer_UpdateDateTime DoorLog : " + varSrc.DateTime + " Insert Success");
                            }
                        }
                        List<DoorLog> listOutSource = listModySource.Where(w => _DateTime.Contains(w.UserID) && _DateTime.Contains(w.CardNo)).Distinct().ToList();
                        logger.Info("OnTimer_UpdateDateTime listOutSource : " + listOutSource.Count);
                        // Out
                        foreach (var varSrc in listOutSource)
                        {
                            // 先判斷WMS中介是否有存在
                            List<DoorLog> listTime = DoorLogRepository.Select(dbHR, transHR, varSrc.CardNo, varSrc.EmployeeID);
                            if (listTime != null && listTime.Count > 0)
                            {
                                // 取得最後一筆
                                DoorLog objTime = listTime.OrderByDescending(w => w.DateTime).FirstOrDefault();
                            
                            }
                            else // 當不存在時,則寫入一筆資料進WMS
                            {
                                varSrc.DateTime = DateTime.Now;
                                DoorLogRepository.Insert(dbHR, transHR, varSrc);
                                logger.Info("OnTimer_UpdateDateTime DoorLog : " + varSrc.DateTime + " Insert Success");
                            }
                        }
                        dbHR.CommitTransaction(transHR);
                        Utility.UpdateSettings("LAST_DateTime", baseDD.ToString("yyyyMMddHHmmss"));
                        logger.Info("OnTimer_UpdateDateTime End");
                    }
                }
                catch (Exception ex)
                {
                    logger.Debug(ex.ToString());
                    if (dbHR != null)
                    {
                        if (transHR != null)
                            dbHR.RollbackTransaction(transHR);
                    }
                }
                finally
                {
                    if (dbERP != null)
                        dbERP.CloseConnection();
                    if (transHR != null)
                        transHR = null;
                    if (dbHR != null)
                        dbHR.CloseConnection();
                }
        }
我的方式參考~
1.先寫好隨時新增到B表的SQL
2.設定從Now得到09、12、15、18、21
3.在09、12、15、18、21的時候,SQL也要記錄09、12、15、18、21時段紀錄的時間(防重複)
4.指定window排程跑每日09、12、15、18、21的時段去寫入~
一定要用寫程式解嗎?
用MSSQL的SQL Agent直接設排程
再搭配SQL語法INSERT INTO SELECT應該就能滿足你的需求?
用C#寫Console模式的程式,會跳出視窗,在專案屬性裡的應用程式頁的輸出類型改成"Windows應用程式",編譯成EXE後放到要進行資料轉進的主機上安裝(最好是做成Click Once安裝),在工作排程上製作要定時執行的程式,執行完轉SQL的工作後即刻結束工作,不要轉圈圈