private void btnDel_Click(object sender, EventArgs e)
        {
            // 刪除
            if (lvStudQuizData.SelectedItems.Count == 1)
            {
                DAO.UDT_StudQuizDataDef sqd = lvStudQuizData.SelectedItems[0].Tag as DAO.UDT_StudQuizDataDef;
                if (sqd != null)
                {
                    if (FISCA.Presentation.Controls.MsgBox.Show("請問刪除測驗資料", "刪除測驗資料", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        List <DAO.UDT_StudQuizDataDef> delData = new List <DAO.UDT_StudQuizDataDef> ();
                        delData.Add(sqd);
                        K12.Data.StudentRecord rec     = K12.Data.Student.SelectByID(PrimaryKey);
                        StringBuilder          logData = new StringBuilder();
                        logData.AppendLine("刪除" + Utility.ConvertString1(rec));
                        logData.AppendLine("測驗名稱「 " + lvStudQuizData.SelectedItems[0].SubItems[0].Text + " 」");
                        if (sqd.ImplementationDate.HasValue)
                        {
                            logData.AppendLine("實施日期:" + sqd.ImplementationDate.Value.ToShortDateString());
                        }
                        if (sqd.AnalysisDate.HasValue)
                        {
                            logData.AppendLine("解析日期:" + sqd.AnalysisDate.Value.ToShortDateString());
                        }
                        XElement elmContent = Utility.ConvertStringToXelm1(sqd.Content);
                        if (elmContent != null)
                        {
                            foreach (XElement elm in elmContent.Elements("Item"))
                            {
                                if (elm.Attribute("name") != null && elm.Attribute("value") != null)
                                {
                                    logData.AppendLine("項目名稱:" + elm.Attribute("name").Value + " , 測驗結果:" + elm.Attribute("value").Value);
                                }
                            }
                        }

                        _UDTTransfer.DeleteStudQuizDataLlist(delData);

                        // log
                        _LogTransfer.SaveLog("學生.輔導相關測驗-刪除", "刪除", "student", PrimaryKey, logData);

                        _BGRun();
                    }
                }
            }
        }
        private void btnDel_Click(object sender, EventArgs e)
        {
            btnDel.Enabled = false;
            if (string.IsNullOrEmpty(cbxQuizName.Text))
            {
                FISCA.Presentation.Controls.MsgBox.Show("請選擇測驗.");
                btnDel.Enabled = true;
                return;
            }

            // 取得所選測驗ID
            int QID = 0;

            if (_QuizIDDict.ContainsKey(cbxQuizName.Text))
            {
                QID = _QuizIDDict[cbxQuizName.Text];
            }

            List <DAO.UDT_StudQuizDataDef> DelList = new List <DAO.UDT_StudQuizDataDef>();

            DelList = (from data in _StudentQuizDataList where data.QuizID == QID select data).ToList();

            if (DelList.Count > 0)
            {
                if (FISCA.Presentation.Controls.MsgBox.Show("請問確定刪除學生測驗資料?", "刪除學生測驗資料", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    _UDTTransfer.DeleteStudQuizDataLlist(DelList);
                    FISCA.Presentation.Controls.MsgBox.Show("刪除完成.");
                    this.Close();
                }
            }
            else
            {
                FISCA.Presentation.Controls.MsgBox.Show("沒有測驗資料.");
            }
            btnDel.Enabled = true;
        }
        /// <summary>
        /// 分批執行匯入
        /// </summary>
        /// <param name="Rows">IRowStream物件列表</param>
        /// <returns>分批匯入完成訊息</returns>
        public override string Import(List <IRowStream> Rows)
        {
            if (mOption.Action == ImportAction.InsertOrUpdate)
            {
                try
                {
                    // log
                    DAO.LogTransfer _logTransfer = new DAO.LogTransfer();

                    List <DAO.UDT_StudQuizDataDef> StudQuizDataList = new List <DAO.UDT_StudQuizDataDef>();
                    List <DAO.UDT_StudQuizDataDef> DelQuizDataList  = new List <DAO.UDT_StudQuizDataDef>();
                    List <DAO.UDT_StudQuizDataDef> HasQuizDataList  = new List <DAO.UDT_StudQuizDataDef>();
                    // 新增 Log
                    Dictionary <int, StringBuilder> Log_Insert = new Dictionary <int, StringBuilder> ();
                    // 刪除 Log
                    Dictionary <int, StringBuilder> Log_Delete = new Dictionary <int, StringBuilder>();


                    List <string> studentNumberList = new List <string>();
                    List <string> ClassSeatNoList   = new List <string>();

                    // 取得試別有學生資料
                    HasQuizDataList = _UDTTransfer.GetStudQuizDataByQuizID(_QuizID);
                    int    count    = 0;
                    string AuthorID = Utility.GetAuthorID();
                    foreach (IRowStream irs in Rows)
                    {
                        count++;
                        this.ImportProgress = count;
                        int sid = 0;
                        // 依學號比對
                        if (irs.Contains("學號") && irs.Contains("狀態"))
                        {
                            if (Global._StudentStatusDBDict.ContainsKey(irs.GetValue("狀態")))
                            {
                                sid = Utility.GetStudentID(irs.GetValue("學號"), irs.GetValue("狀態"));
                                studentNumberList.Add("學號:" + irs.GetValue("學號"));
                            }
                        }

                        // 依班座比對
                        if (irs.Contains("班級") && irs.Contains("座號") && irs.Contains("狀態"))
                        {
                            if (Global._StudentStatusDBDict.ContainsKey(irs.GetValue("狀態")))
                            {
                                sid = Utility.GetStudentID(irs.GetValue("班級"), irs.GetValue("座號"), irs.GetValue("狀態"));
                                ClassSeatNoList.Add("班級:" + irs.GetValue("班級") + ",座號:" + irs.GetValue("座號"));
                            }
                        }

                        // 比對需要刪除
                        foreach (DAO.UDT_StudQuizDataDef data in HasQuizDataList.Where(x => x.StudentID == sid))
                        {
                            DelQuizDataList.Add(data);
                        }



                        DAO.UDT_StudQuizDataDef sqd = new DAO.UDT_StudQuizDataDef();
                        sqd.QuizID    = int.Parse(_QuizID);
                        sqd.AuthorID  = AuthorID;
                        sqd.StudentID = sid;

                        DateTime dt1, dt2;
                        if (irs.Contains("實施日期"))
                        {
                            if (DateTime.TryParse(irs.GetValue("實施日期"), out dt1))
                            {
                                sqd.ImplementationDate = dt1;
                            }
                        }

                        if (irs.Contains("解析日期"))
                        {
                            if (DateTime.TryParse(irs.GetValue("解析日期"), out dt2))
                            {
                                sqd.AnalysisDate = dt2;
                            }
                        }

                        List <XElement> elmList = new List <XElement>();
                        foreach (string str in _DataFieldNameList)
                        {
                            if (irs.Contains(str))
                            {
                                XElement elm = new XElement("Item");
                                elm.SetAttributeValue("name", str);
                                elm.SetAttributeValue("value", irs.GetValue(str));
                                elmList.Add(elm);
                            }
                        }
                        sqd.Content = Utility.ConvertXmlListToString1(elmList);

                        StudQuizDataList.Add(sqd);
                    }

                    if (DelQuizDataList.Count > 0)
                    {
                        List <int> intList = (from data in DelQuizDataList select data.StudentID).ToList();
                        Dictionary <int, string> studNameDict = Utility.GetConvertStringDict1fromDB(intList);

                        // 收集 Log
                        foreach (DAO.UDT_StudQuizDataDef data in DelQuizDataList)
                        {
                            if (studNameDict.ContainsKey(data.StudentID))
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine("匯入輔導測驗-刪除");
                                sb.AppendLine(studNameDict[data.StudentID]);
                                sb.AppendLine("測驗名稱:" + _QuizName);
                                if (data.ImplementationDate.HasValue)
                                {
                                    sb.AppendLine("實施日期:" + data.ImplementationDate.Value.ToShortDateString());
                                }

                                if (data.AnalysisDate.HasValue)
                                {
                                    sb.AppendLine("解析日期:" + data.AnalysisDate.Value.ToShortDateString());
                                }

                                XElement elmContent = Utility.ConvertStringToXelm1(data.Content);
                                if (elmContent != null)
                                {
                                    foreach (XElement elm in elmContent.Elements("Item"))
                                    {
                                        if (elm.Attribute("name") != null && elm.Attribute("value") != null)
                                        {
                                            sb.AppendLine("項目名稱:" + elm.Attribute("name").Value + " , 測驗結果:" + elm.Attribute("value").Value);
                                        }
                                    }
                                }

                                if (!Log_Delete.ContainsKey(data.StudentID))
                                {
                                    Log_Delete.Add(data.StudentID, sb);
                                }
                            }
                        }

                        _UDTTransfer.DeleteStudQuizDataLlist(DelQuizDataList);
                    }
                    if (StudQuizDataList.Count > 0)
                    {
                        List <int> intList = (from data in StudQuizDataList select data.StudentID).ToList();
                        Dictionary <int, string> studNameDict = Utility.GetConvertStringDict1fromDB(intList);

                        // 收集 Log
                        foreach (DAO.UDT_StudQuizDataDef data in StudQuizDataList)
                        {
                            if (studNameDict.ContainsKey(data.StudentID))
                            {
                                StringBuilder sb = new StringBuilder();
                                // 檢查是否有刪除,有放上面
                                if (Log_Delete.ContainsKey(data.StudentID))
                                {
                                    sb.AppendLine(Log_Delete[data.StudentID].ToString());
                                    sb.AppendLine();
                                }

                                sb.AppendLine("匯入輔導測驗-新增");
                                sb.AppendLine(studNameDict[data.StudentID]);
                                sb.AppendLine("測驗名稱:" + _QuizName);
                                if (data.ImplementationDate.HasValue)
                                {
                                    sb.AppendLine("實施日期:" + data.ImplementationDate.Value.ToShortDateString());
                                }

                                if (data.AnalysisDate.HasValue)
                                {
                                    sb.AppendLine("解析日期:" + data.AnalysisDate.Value.ToShortDateString());
                                }

                                XElement elmContent = Utility.ConvertStringToXelm1(data.Content);
                                if (elmContent != null)
                                {
                                    foreach (XElement elm in elmContent.Elements("Item"))
                                    {
                                        if (elm.Attribute("name") != null && elm.Attribute("value") != null)
                                        {
                                            sb.AppendLine("項目名稱:" + elm.Attribute("name").Value + " , 測驗結果:" + elm.Attribute("value").Value);
                                        }
                                    }
                                }

                                if (!Log_Insert.ContainsKey(data.StudentID))
                                {
                                    Log_Insert.Add(data.StudentID, sb);
                                }
                            }
                        }

                        _UDTTransfer.InsertStudQuizDataLlist(StudQuizDataList);
                    }

                    // log 資料
                    if (Log_Insert.Count > 0)
                    {
                        foreach (KeyValuePair <int, StringBuilder> data in Log_Insert)
                        {
                            _logTransfer.SaveLog("輔導系統-匯入測驗資料", "匯入", "student", data.Key.ToString(), data.Value);
                        }
                    }

                    // 總共
                    StringBuilder logData = new StringBuilder();
                    logData.AppendLine("測驗名稱:" + _QuizName);
                    if (ClassSeatNoList.Count > 0)
                    {
                        logData.AppendLine("--依班級座號匯入--");
                        foreach (string str in ClassSeatNoList)
                        {
                            logData.AppendLine(str);
                        }

                        logData.AppendLine("匯入學生共" + ClassSeatNoList.Count + "人");
                    }
                    if (studentNumberList.Count > 0)
                    {
                        logData.AppendLine("--依學號匯入--");
                        foreach (string str in studentNumberList)
                        {
                            logData.AppendLine(str);
                        }

                        logData.AppendLine("匯入學生共" + studentNumberList.Count + "人");
                    }
                    // 寫入 log
                    _logTransfer.SaveLog("輔導系統.匯入測驗資料", "匯入", "", "", logData);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return("");
        }