public bool CopyToNewStore(Hashtable htParams, DbTransaction RootDBT)
        {
            #region
            bool IsRootTranscation = false;

            bool bResult = false;
            Int32 PID = 0;
            Hashtable htParamsMaster = new Hashtable();
            DataTable dtForCopyDetail = new DataTable();
            DataRow dr;
            dtForCopyDetail.Columns.Add("ID");
            dtForCopyDetail.Columns.Add("CREATEDATE");
            dtForCopyDetail.Columns.Add("CREATEUID");
            dtForCopyDetail.Columns.Add("UPDATEDATE");
            dtForCopyDetail.Columns.Add("UPDATEUID");
            dtForCopyDetail.Columns.Add("START_RANKQTY");
            dtForCopyDetail.Columns.Add("END_RANKQTY");
            dtForCopyDetail.Columns.Add("ADJ_QTY");

            string strCurrentId = "", strLastId = "";

            try
            {
                VDS_ALO_PATTERNSPEC_MAIN_DBO ALOM = new VDS_ALO_PATTERNSPEC_MAIN_DBO(ref USEDB);
                VDS_ALO_PATTERNSPEC_DETL_DBO ALOD = new VDS_ALO_PATTERNSPEC_DETL_DBO(ref USEDB);


                #region    取得相關資料

                DataTable dtForCopy = ALOM.doQueryForCopyToNewStore(htParams);
                Hashtable htTemp = new Hashtable();
                string strErrMsg = "";
                if (dtForCopy != null && dtForCopy.Rows.Count > 0)
                {
                    for (Int32 i = dtForCopy.Rows.Count - 1; i >= 0; i--)
                    {
                        htTemp.Clear();
                        htTemp.Add("ChanNo", dtForCopy.Rows[i]["new_chan_no"].ToString().Trim());
                        htTemp.Add("Store", dtForCopy.Rows[i]["new_store"].ToString().Trim());

                        if (dtForCopy.Rows[i]["s_end_date"] != DBNull.Value)
                            htTemp.Add("StartDate", ((DateTime)dtForCopy.Rows[i]["s_start_date"]).ToString("yyyy/MM/dd"));
                        else
                            htTemp.Add("StartDate", null);

                        if (dtForCopy.Rows[i]["s_end_date"] != DBNull.Value)
                            htTemp.Add("EndDate", ((DateTime)dtForCopy.Rows[i]["s_end_date"]).ToString("yyyy/MM/dd"));
                        else
                            htTemp.Add("EndDate", null);

                        if (!CheckPATTERNSPECCanAdd(htTemp, ref strErrMsg))
                        {
                            dtForCopy.Rows.RemoveAt(i);
                        }
                    }
                }
                else { throw new Exception("複製失敗,無資料可複製!"); }

                #endregion

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;

                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                if (dtForCopy != null && dtForCopy.Rows.Count > 0)
                {

                    for (Int32 i = 0; i <= dtForCopy.Rows.Count - 1; i++)
                    {
                        #region

                        //MASTER
                        strCurrentId = dtForCopy.Rows[i]["ID"].ToString().Trim();

                        if (strCurrentId != strLastId)
                        {
                            htParamsMaster.Clear();
                            htParamsMaster.Add("CODE", dtForCopy.Rows[i]["new_code"]);
                            htParamsMaster.Add("UserID", dtForCopy.Rows[i]["createuid"]);
                            htParamsMaster.Add("ChanNO", dtForCopy.Rows[i]["new_chan_no"]);
                            htParamsMaster.Add("STORE", dtForCopy.Rows[i]["new_store"]);
                            htParamsMaster.Add("ROOT_NO", dtForCopy.Rows[i]["ROOT_NO"]);
                            htParamsMaster.Add("PMA", dtForCopy.Rows[i]["PMA"]);
                            htParamsMaster.Add("PATTERN_NO", dtForCopy.Rows[i]["PATTERN_NO"]);

                            htParamsMaster.Add("SPEC_TYPE", dtForCopy.Rows[i]["spec_type"]);
                            htParamsMaster.Add("SPEC_QTY", dtForCopy.Rows[i]["spec_qty"]);
                            htParamsMaster.Add("S_START_DATE", dtForCopy.Rows[i]["s_start_date"]);
                            htParamsMaster.Add("S_END_DATE", dtForCopy.Rows[i]["s_end_date"]);
                            htParamsMaster.Add("REASON", dtForCopy.Rows[i]["reason"]);

                            //DETAIL
                            dtForCopyDetail.Clear();

                            #region 記錄明細
                            DataRow[] dRows = dtForCopy.Select("ID=" + strCurrentId + " and START_RANKQTY is not null");
                            foreach (DataRow oRow in dRows)
                            {
                                dr = dtForCopyDetail.NewRow();
                                #region old
                                //dr["ID"] = strCurrentId;
                                //dr["CREATEDATE"] = DateTime.Now;
                                //dr["CREATEUID"] = dtForCopy.Rows[k]["createuid"];
                                //dr["UPDATEDATE"] = DateTime.Now;
                                //dr["UPDATEUID"] = dtForCopy.Rows[k]["updateuid"];
                                //dr["START_RANKQTY"] = dtForCopy.Rows[k]["start_rankqty"];
                                //dr["END_RANKQTY"] = dtForCopy.Rows[k]["end_rankqty"];
                                //dr["ADJ_QTY"] = dtForCopy.Rows[k]["adj_qty"];
                                #endregion

                                #region 新增明細
                                dr["ID"] = strCurrentId;
                                dr["CREATEDATE"] = DateTime.Now;
                                dr["CREATEUID"] = oRow["createuid"];
                                dr["UPDATEDATE"] = DateTime.Now;
                                dr["UPDATEUID"] = oRow["updateuid"];
                                dr["START_RANKQTY"] = oRow["start_rankqty"];
                                dr["END_RANKQTY"] = oRow["end_rankqty"];
                                dr["ADJ_QTY"] = oRow["adj_qty"];
                                #endregion

                                dtForCopyDetail.Rows.Add(dr);
                            }
                            #endregion

                            #region 處理新增
                            PID = CreateMaster(htParamsMaster, DBT);
                            if (PID != 0)
                            {
                                if (dtForCopyDetail != null)
                                    CreateDetail(PID, dtForCopyDetail, DBT);
                            }
                            else
                            {
                                throw new Exception("複製0筆資料!");
                            }
                            #endregion

                        }

                        strLastId = dtForCopy.Rows[i]["ID"].ToString().Trim();

                        #endregion
                    }


                    #region 交易成功

                    if (IsRootTranscation)
                    {
                        //獨立呼叫Transcation成立
                        DBT.Commit();
                    }

                    #endregion


                    bResult = true;

                }
                else
                {
                    bResult = false; 
                }

                return bResult;

            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw GetNewException(ex);
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }
                #endregion
            }
            #endregion
        }
        /// <summary>
        /// 基本表單刪除BCO
        /// </summary>
        /// <param name="htParams">刪除變數</param>
        /// <param name="RootDBT">是否有主交易,無主交易輸入null</param>
        /// <returns>回傳影響筆數</returns>
        public bool DeleteDetailAndLog(Hashtable htParams,
                                      DbTransaction RootDBT
                                      )
        {
            bool IsRootTranscation = false;
            bool bResult = false;

            try
            {
                VDS_ALO_PATTERNSPEC_DETL_DBO ALOD = new VDS_ALO_PATTERNSPEC_DETL_DBO(ref USEDB);

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;

                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                ALOD.doDelAndLogbyPID(htParams, DBT);

                #region 交易成功

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation成立
                    DBT.Commit();
                }

                #endregion


                bResult = true;
                return bResult;

            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw GetNewException(ex);
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }

                #endregion

            }


        }
        /// <summary>
        /// 基本表單新增BCO
        /// </summary>
        /// <param name="DetailData">輸入變數</param>
        /// <param name="RootDBT">是否有主交易,無主交易輸入null</param>
        /// <returns>回傳異動筆數</returns>
        public void CreateDetail(Int32 PID, DataTable DetailData, DbTransaction RootDBT)
        {
            bool IsRootTranscation = false;
            try
            {
                VDS_ALO_PATTERNSPEC_DETL_DBO ALOD = new VDS_ALO_PATTERNSPEC_DETL_DBO(ref USEDB);

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;


                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                Hashtable htParams = new Hashtable();

                for (Int32 i = 0; i <= DetailData.Rows.Count - 1; i++)
                {
                    if (DetailData.Rows[i].RowState != DataRowState.Deleted)
                    {
                        htParams.Clear();
                        htParams.Add("PID", PID);
                        htParams.Add("ID", DetailData.Rows[i]["ID"]);
                        htParams.Add("CREATEDATE", DetailData.Rows[i]["CREATEDATE"]);
                        htParams.Add("CREATEUID", DetailData.Rows[i]["CREATEUID"]);
                        htParams.Add("UPDATEDATE", DetailData.Rows[i]["UPDATEDATE"]);
                        htParams.Add("UPDATEUID", DetailData.Rows[i]["UPDATEUID"]);
                        htParams.Add("START_RANKQTY", DetailData.Rows[i]["START_RANKQTY"]);
                        htParams.Add("END_RANKQTY", DetailData.Rows[i]["END_RANKQTY"]);
                        htParams.Add("ADJ_QTY", DetailData.Rows[i]["ADJ_QTY"]);
                        ALOD.doCreate(htParams, DBT);
                    }
                }

                #region 交易成功

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation成立
                    DBT.Commit();
                }

                #endregion

            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw GetNewException(ex);
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }

                #endregion

            }
        }
        /// <summary>
        /// 基本表單更新BCO
        /// </summary>
        /// <param name="DetailData">更新變數</param>
        /// <param name="RootDBT">是否有主交易,無主交易輸入null</param>
        /// <returns>回傳成功與否</returns>
        public bool UpdateDetail(DataTable dtForDetailAddedOrModified, string strUIDForLog, DataTable dtForDetailDeleted, DbTransaction RootDBT)
        {
            bool IsRootTranscation = false;
            bool bResult = false;
            try
            {
                VDS_ALO_PATTERNSPEC_DETL_DBO ALOD = new VDS_ALO_PATTERNSPEC_DETL_DBO(ref USEDB);

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;


                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                Hashtable htParams = new Hashtable();


                if (dtForDetailDeleted != null)
                {
                    #region
                    for (Int32 i = 0; i <= dtForDetailDeleted.Rows.Count - 1; i++)
                    {
                        if (dtForDetailDeleted.Rows[i].RowState != DataRowState.Deleted)
                        {
                            #region
                            htParams.Clear();
                            htParams.Add("ID", dtForDetailDeleted.Rows[i]["ID"]);
                            htParams.Add("PID", dtForDetailDeleted.Rows[i]["PID"]);
                            htParams.Add("CREATEDATE", dtForDetailDeleted.Rows[i]["CREATEDATE"]);
                            htParams.Add("CREATEUID", dtForDetailDeleted.Rows[i]["CREATEUID"]);
                            htParams.Add("UPDATEDATE", dtForDetailDeleted.Rows[i]["UPDATEDATE"]);
                            htParams.Add("UPDATEUID", dtForDetailDeleted.Rows[i]["UPDATEUID"]);
                            htParams.Add("START_RANKQTY", dtForDetailDeleted.Rows[i]["START_RANKQTY"]);
                            htParams.Add("END_RANKQTY", dtForDetailDeleted.Rows[i]["END_RANKQTY"]);
                            htParams.Add("ADJ_QTY", dtForDetailDeleted.Rows[i]["ADJ_QTY"]);
                            htParams.Add("UIDForLog", strUIDForLog);
                            ALOD.doDelete(htParams, DBT);
                            #endregion
                        }
                    }
                    #endregion
                }

                if (dtForDetailAddedOrModified != null)
                {
                    #region
                    for (Int32 i = 0; i <= dtForDetailAddedOrModified.Rows.Count - 1; i++)
                    {
                        if ((dtForDetailAddedOrModified.Rows[i].RowState == DataRowState.Added) || (dtForDetailAddedOrModified.Rows[i].RowState == DataRowState.Modified))
                        {
                            #region
                            htParams.Clear();
                            htParams.Add("ID", dtForDetailAddedOrModified.Rows[i]["ID"]);
                            htParams.Add("PID",dtForDetailAddedOrModified.Rows[i]["PID"]);
                            htParams.Add("CREATEDATE",dtForDetailAddedOrModified.Rows[i]["CREATEDATE"]);
                            htParams.Add("CREATEUID",dtForDetailAddedOrModified.Rows[i]["CREATEUID"]);
                            htParams.Add("UPDATEDATE",dtForDetailAddedOrModified.Rows[i]["UPDATEDATE"]);
                            htParams.Add("UPDATEUID",dtForDetailAddedOrModified.Rows[i]["UPDATEUID"]);
                            htParams.Add("START_RANKQTY", dtForDetailAddedOrModified.Rows[i]["START_RANKQTY"]);
                            htParams.Add("END_RANKQTY", dtForDetailAddedOrModified.Rows[i]["END_RANKQTY"]);
                            htParams.Add("ADJ_QTY", dtForDetailAddedOrModified.Rows[i]["ADJ_QTY"]);

                            ALOD.doCreate(htParams, DBT);
                            #endregion
                        }
                    }
                    #endregion
                }


                #region 交易成功

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation成立
                    DBT.Commit();
                }

                #endregion

                bResult = true;

                return bResult;
            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw GetNewException(ex);
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }

                #endregion

            }
        }
 public DataTable QueryDetlByPID(Hashtable htParams)
 {
     #region
     try
     {
         VDS_ALO_PATTERNSPEC_DETL_DBO ALOD = new VDS_ALO_PATTERNSPEC_DETL_DBO(ref USEDB);
         return ALOD.doQueryDetlByPID(htParams);
     }
     catch (Exception ex)
     {
         throw GetNewException(ex);
     }
     #endregion
 }