Beispiel #1
0
    /// <summary>
    /// 新增EmpGuardWorkTime資料
    /// </summary>
    /// <param name="model">畫面model</param>
    /// <param name="msg">回傳訊息</param>
    /// <returns>bool</returns>
    public static bool AddEmpGuardWorkTime(EmpGuardWorkTimeBean dataBean, out string msg)
    {
        bool result = false;

        msg = "";
        try
        {
            using (var conn = new SqlConnection()
            {
                ConnectionString = DbHelper.getConnectionStrings(_attendantDBName).ConnectionString
            })
            {
                conn.Open();

                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var count = conn.Query <EmpGuardWorkTimeBean>(WorkTimeSql.SelectEmpGuardWorkTime(), dataBean, trans).Count(); //執行查詢,結果回傳至TestBean物件
                        if (count == 0)
                        {
                            conn.Execute(WorkTimeSql.AddEmpGuardWorkTime(), dataBean, trans); //執行修改,並做Transaction機制
                        }
                        else
                        {
                            throw new Exception("資料已存在,請勿重複新增!!");
                        }

                        trans.Commit(); //成功Transaction直接Commit
                    }
                    catch (Exception)
                    {
                        trans.Rollback(); //失敗Transaction Rollback
                        result = false;
                        throw;
                    }
                }
            }
            result = true;
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        return(result);
    }