コード例 #1
0
ファイル: WorkTime.cs プロジェクト: k741030ken/Attendant
    /// <summary>
    /// 新增EmpWorkTime資料
    /// </summary>
    /// <param name="model">畫面model</param>
    /// <param name="msg">回傳訊息</param>
    /// <returns>bool</returns>
    public static bool AddEmpWorkTime(List <EmpWorkTimeBean> 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
                    {
                        foreach (var item in dataBean)
                        {
                            var count = conn.Query <EmpWorkTimeBean>(WorkTimeSql.SelectEmpWorkTime(), item, trans).Count(); //執行查詢,結果回傳至TestBean物件
                            if (count == 0)
                            {
                                conn.Execute(WorkTimeSql.AddEmpWorkTime(), item, trans); //執行新增,並做Transaction機制
                            }
                            else
                            {
                                conn.Execute(WorkTimeSql.UpdateEmpWorkTime(), item, trans); //執行修改,並做Transaction機制
                            }
                        }
                        trans.Commit(); //成功Transaction直接Commit
                    }
                    catch (Exception)
                    {
                        trans.Rollback(); //失敗Transaction Rollback
                        result = false;
                        throw;
                    }
                }
            }
            result = true;
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        return(result);
    }