public ActionResult LisLaboratoryMaster_Update([DataSourceRequest] DataSourceRequest request, ViewModel_LisLaboratoryMaster crud)
        {
            if (crud != null)
            {
                if (ModelState.IsValid)
                {
                    var entity = new lisLaboratoryMaster
                    {
                        LLMRowid  = crud.LLMRowid,
                        LLMName   = crud.LLMName,
                        LLMFormat = crud.LLMFormat
                    };

                    db_zmlis.lisLaboratoryMaster.Attach(entity);
                    db_zmlis.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                    db_zmlis.SaveChanges();
                }
            }

            return(Json(new[] { crud }.ToDataSourceResult(request, ModelState)));
        }
        public ActionResult LisLaboratoryMaster_Destroy([DataSourceRequest] DataSourceRequest request, lisLaboratoryMaster crud)
        {
            if (ModelState.IsValid)
            {
                string sLLMRowid = crud.LLMRowid;

                var entity = new lisLaboratoryMaster
                {
                    LLMRowid = crud.LLMRowid
                };

                db_zmlis.lisLaboratoryMaster.Attach(entity);
                db_zmlis.lisLaboratoryMaster.Remove(entity);
                db_zmlis.SaveChanges();

                // 明細資料要一併刪除
                var delete_items = db_zmlis.lisLaboratoryDetail.Where(x => sLLMRowid.Contains(x.LLMRowid));
                db_zmlis.lisLaboratoryDetail.RemoveRange(delete_items);
                db_zmlis.SaveChanges();
            }

            return(Json(new[] { crud }.ToDataSourceResult(request, ModelState)));
        }
        public ActionResult LisLaboratoryMaster_Create([DataSourceRequest] DataSourceRequest request, ViewModel_LisLaboratoryMaster crud)
        {
            string sLLMRowid = Guid.NewGuid().ToString();

            if (ModelState.IsValid)
            {
                var entity = new lisLaboratoryMaster
                {
                    LLMRowid  = sLLMRowid,
                    LLMName   = crud.LLMName,
                    LLMFormat = crud.LLMFormat
                };

                db_zmlis.lisLaboratoryMaster.Add(entity);
                db_zmlis.SaveChanges();
                crud.LLMRowid = entity.LLMRowid;
            }

            // 產生檢驗結構
            List <lisPatientLaboratorySchemaClass> llsc = new List <lisPatientLaboratorySchemaClass>();

            // 定義檢驗表頭資料結構
            //lisPatientLaboratorySchemaClass lplsc = new lisPatientLaboratorySchemaClass();
            //lplsc.LLSCode = "PLMPTIdno";
            //lplsc.LLSFieldName = "身份證字號";
            //lplsc.LLDFieldType = "NVARCHAR";
            //lplsc.LLDFieldLength = 50;
            //lplsc.LLDFieldFloatLength = 0;
            //lplsc.LLDFieldKind = "B";
            //lplsc.LLDTextStartPos = 0;
            //lplsc.LLDTextEndPos = 0;
            //lplsc.LLDMappingField = "";
            //lplsc.LLDFieldMemo = "";
            //lplsc.LLDSeqno = 0;

            string sbconn = String.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True; User ID={2}; Password={3}", "zmcms.cloud,1933", "ZMLIS", "sa", "I@ntif@t");


            // 讀取表頭結構
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMPTIdno", LLDFieldName = "身份證字號", LLDFieldType = "NVARCHAR", LLDFieldLength = 10, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 1
            });;
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMPTName", LLDFieldName = "病人姓名", LLDFieldType = "NVARCHAR", LLDFieldLength = 20, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 2
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMPTBirthday", LLDFieldName = "出生日期", LLDFieldType = "DATETIME", LLDFieldLength = 0, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 3
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMPTGender", LLDFieldName = "性別", LLDFieldType = "NVARCHAR", LLDFieldLength = 10, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 4
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMPTCode", LLDFieldName = "原始病歷號碼", LLDFieldType = "NVARCHAR", LLDFieldLength = 20, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 5
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMClinicDate", LLDFieldName = "就醫日期", LLDFieldType = "DATETIME", LLDFieldLength = 0, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 6
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMApplyDate", LLDFieldName = "收件日期", LLDFieldType = "DATETIME", LLDFieldLength = 0, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 7
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMApplyTime", LLDFieldName = "收件時間", LLDFieldType = "NVARCHAR", LLDFieldLength = 8, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 8
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMInspDate", LLDFieldName = "檢驗日期", LLDFieldType = "DATETIME", LLDFieldLength = 0, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 9
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMInspTime", LLDFieldName = "檢驗時間", LLDFieldType = "NVARCHAR", LLDFieldLength = 8, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 10
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMReportDate", LLDFieldName = "報告日期", LLDFieldType = "DATETIME", LLDFieldLength = 0, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 11
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMReportTime", LLDFieldName = "報告時間", LLDFieldType = "NVARCHAR", LLDFieldLength = 8, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 12
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMSNo", LLDFieldName = "檢驗單號", LLDFieldType = "NVARCHAR", LLDFieldLength = 50, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 13
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLMReqno", LLDFieldName = "檢驗單序號", LLDFieldType = "NVARCHAR", LLDFieldLength = 20, LLDFieldFloatLength = 0, LLDFieldKind = "H", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 14
            });

            // 讀取表身結構
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLDCode", LLDFieldName = "檢驗項目代碼", LLDFieldType = "NVARCHAR", LLDFieldLength = 30, LLDFieldFloatLength = 0, LLDFieldKind = "B", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 50
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLDName", LLDFieldName = "檢驗項目名稱", LLDFieldType = "NVARCHAR", LLDFieldLength = 255, LLDFieldFloatLength = 0, LLDFieldKind = "B", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 51
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLDValue", LLDFieldName = "檢驗值", LLDFieldType = "FLOAT", LLDFieldLength = 0, LLDFieldFloatLength = 0, LLDFieldKind = "B", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 52
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLDType", LLDFieldName = "異常代碼", LLDFieldType = "NVARCHAR", LLDFieldLength = 50, LLDFieldFloatLength = 0, LLDFieldKind = "B", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 53
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLDUnit", LLDFieldName = "檢驗單位", LLDFieldType = "NVARCHAR", LLDFieldLength = 20, LLDFieldFloatLength = 0, LLDFieldKind = "B", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = true, LLDSeqno = 54
            });
            llsc.Add(new lisPatientLaboratorySchemaClass {
                LLDRowid = Guid.NewGuid().ToString(), LLMRowid = sLLMRowid, LLDCode = "PLDMemo", LLDFieldName = "檢驗註記", LLDFieldType = "NVARCHAR", LLDFieldLength = 20, LLDFieldFloatLength = 0, LLDFieldKind = "B", LLDTextStartPos = 0, LLDTextEndPos = 0, LLDMappingField = "", LLDFieldMemo = "", LLDTransferFormat = "", LLDRequiredFields = false, LLDSeqno = 55
            });

            // 儲存至 Table
            string  lld_json = JsonConvert.SerializeObject(new { Table = llsc });
            DataSet ds_lld   = JsonConvert.DeserializeObject <DataSet>(lld_json);

            using (SqlBulkCopy bcp_llm = new SqlBulkCopy(sbconn))
            {
                bcp_llm.DestinationTableName = "lisLaboratoryDetail";

                foreach (DataTable table in ds_lld.Tables)
                {
                    foreach (DataColumn column in table.Columns)
                    {
                        bcp_llm.ColumnMappings.Add(column.ColumnName, column.ColumnName);
                    }
                }

                bcp_llm.WriteToServer(ds_lld.Tables[0]);
            }

            return(Json(new[] { crud }.ToDataSourceResult(new DataSourceRequest(), ModelState)));
        }