Monday, March 02, 2015

[C#] Delegate 練習 2

Delegate 簡易練習
public partial class _Default : System.Web.UI.Page
{

    delegate string demo(string Name);
 
    class Say
    {

        public static string SayHello (string name)
        {
            return string.Format("Hello {0}", name);
        }

        public static string HelloHey (string name)
        {
            return string.Format("Hey {0}", name);
        }
    }

    protected void btnRun_Click(object sender, EventArgs e)
    {
        demo d1 = new demo(Say.SayHello);
        lblResult.Text = d1("JengTing") + "<br>";
        demo d2 = new demo(Say.HelloHey);
        lblResult.Text += d2("JengTing") + "<br>";  
    }
}

[C#] Delegate 練習 2

Thursday, February 26, 2015

0Y9M - 爬行比賽

硯人生的第一次競賽

規定要穿上主辦單位的衣服

0Y9M-爬行比賽

Monday, February 23, 2015

[C#] Delegate 練習

遠距教學提到可以透過 delegate 把 Method 當成參數傳進 Method 中,故意拿勞保費計算來練習 delegate

畫面控件
  • 勞保投保薪資:25200、33300、43900
  • 減免條件:正常、身心輕度、身心中度、極重或重

[C#] Delegate 練習-1

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
    }

    delegate decimal demo(decimal LSalary , decimal discount);

    class Employ
    {
        public bool Hirer { get; set; }
        
        // 計算勞保薪資
        public decimal LaborFee(decimal LSalary , decimal discount,demo func)            
        {                 
            decimal retValue = 0m;                 

            if (this.Hirer == true)                 
            {                      
                retValue = func(LSalary , discount);                 
            }                 
            else                 
            {                      
                retValue = func(LSalary, discount);                 
            }                 
            return retValue;            
        }              
      
        // 勞保計算公式 - 雇主
        public decimal LaborForHirer(decimal LSalary, decimal discount)
        {

            decimal retValue = Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) -
                               Math.Round
                                   (
                                       (
                                           Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) +
                                           Math.Round(LSalary * 0.2m, 0, MidpointRounding.AwayFromZero)
                                       ) * discount
                                       , 0, MidpointRounding.AwayFromZero
                                   );

            if (retValue < 0) retValue = 0;
            return retValue;
        }

        // 勞保計算公式 - 員工
        public decimal LaborForEmploy(decimal LSalary, decimal discount)
        {
            decimal retValue = Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) +
                               Math.Round(LSalary * 0.01m * 0.2m, 0, MidpointRounding.AwayFromZero) -
                               Math.Round
                                   (
                                       (
                                           Math.Round(LSalary * 0.09m * 0.2m, 0, MidpointRounding.AwayFromZero) +
                                           Math.Round(LSalary * 0.01m * 0.2m, 0, MidpointRounding.AwayFromZero)
                                       ) * discount
                                       , 0, MidpointRounding.AwayFromZero
                                   );

            if (retValue < 0) retValue = 0;
            return retValue;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Employ emp = new Employ();
        emp.Hirer = chkHirer.Checked;
        decimal LSalary = Convert.ToDecimal(ddlLSalary.SelectedValue);

        decimal discount = 0m;
        string discountType = ddldiscountType.SelectedValue;
        switch (discountType)
        {
            case "正常":
                discount = 0m;
                break ;
            case "身心輕度":
                discount = 0.25m;
                break ;
            case "身心中度":
                discount = 0.5m;
                break ;
            case "極重或重":
                discount = 1m;
                break;
            default:
                discount = 1m;
                break;
        }

        demo func;
        if (emp.Hirer == true)
        {
            func = new demo(emp.LaborForHirer);
        }
        else
        {
            func = new demo(emp.LaborForEmploy);
        }

        lblResult.Text = string.Format("勞保費為:{0}", emp.LaborFee(LSalary, discount, func));
    }
}

顯示結果
[C#] Delegate 練習-2

Sunday, February 22, 2015

0Y9M - 呆呆向前衝

原本是想說拍攝硯爬過來的情況,沒想到意外拍到他走路的畫面,而且還一次走這麼遠,不過我猜看到這影片的朋友,應該都會提醒我小朋友太早走路,對骨頭發展不好吧 ~~ ^^''


Tuesday, February 17, 2015

[SQL] 管理錯誤記錄檔案

這篇記錄 xp_readerrorlog(undocumented extended stored procedure)搭配 Database Mail 檢查每天是否有錯誤訊息並發信通知
-- 宣告變數
DECLARE @text NVARCHAR(MAX)
DECLARE @ErrorLog TABLE (ID INT IDENTITY, LogDate datetime, ProcessInfo nvarchar(10),Text nvarchar(3950))
DECLARE @StartDate AS datetime
DECLARE @EndDate AS datetime
SET @StartDate = CONVERT(char(10),getdate(),120)
SET @EndDate = CONVERT(char(10),DATEADD(dd,1,getdate()),120)

-- 把 SQL Server ErrorLog 資料塞進 @ErrorLog
INSERT @ErrorLog EXEC master.dbo.xp_readerrorlog 0,1,NULL,NULL,@StartDate,@EndDate

-- 把 SQL Server Agent ErrorLog 資料塞進 @ErrorLog
INSERT @ErrorLog EXEC master.dbo.xp_readerrorlog 0,2,NULL,NULL,@StartDate,@EndDate

IF EXISTS 
    (
        SELECT 1 
        FROM @ErrorLog 
        WHERE Text LIKE N'%錯誤%' OR 
            (Text LIKE N'%Error%' AND Text NOT LIKE N'%SPN%')
    )
        BEGIN
        
            -- 整理 HTML Table 資料
            SET @text =
                N' <H1>錯誤檔案</H1>' +
                N'<table border="1">' +
                N'<tr><th>ID</th><th>LogDate</th><th>ProcessInfo</th><th>Text</th></tr>' +
                ( 
                    SELECT
                        td = ID, '' ,
                        td = LogDate, '' ,
                        td = ProcessInfo, '' ,
                        td = Text
                    FROM @ErrorLog 
                    WHERE Text LIKE N'%錯誤%' OR 
                        (Text LIKE N'%Error%' AND Text NOT LIKE N'%SPN%')
                    FOR XML PATH('tr')
                ) +
                N'</table>'

            -- 進行發信通知  
            EXEC msdb.dbo.sp_send_dbmail 
                @profile_name = 'ProfileName' ,
                @recipients = '收件人 EMail' ,
                @subject = 'SQL Server 通知 - 錯誤檔案訊息',
                @body = @text ,
                @body_format = 'HTML'

        END
故意利用 sa 登入造成登入失敗,再利用上述語法來測試是否發信成功
[SQL] 管理錯誤記錄檔案