protected void btnAddConfig_Click(object sender, DirectEventArgs e) { try { OneManyFormInfo info = new OneManyFormInfo(); info.Title = txtTitle.Text; info.TableName = cbTable.SelectedItem.Value; info.Order = 0; info.Visible = chkVisible.Checked; info.ForeignKey = cbForeignKey.SelectedItem.Value; info.OneManyFormName = this.GridPanelName + "OneManyForm"; OneManyFormController.GetInstance().Add(info); } catch (Exception ex) { Dialog.ShowError(ex.Message); } }
private void InsertDetailTableValue(List <MiniGridData> miniGridDataList, string ForeignKeyValue) { List <OneManyFormInfo> formList = OneManyFormController.GetInstance().GetAll(this.GridPanelName + "OneManyForm", 1); //trong chuối json đã có sẵn tên cột, chỉ cần replace lại là ok foreach (var item in miniGridDataList) { if (!string.IsNullOrEmpty(item.JsonData)) { string tableName = item.MiniGridName.Replace(this.GridPanelName + "_OneManyForm_", ""); OneManyFormInfo frmTmp = (from t in formList where t.TableName == tableName select t).FirstOrDefault(); string sqlUpdate = "update {1} set {0} where " + frmTmp.PrimaryKey + " = N'"; string sqlInsert = "insert into {2}({0}) values ({1})"; string checkSql = ""; //cần phải viết lại hàm tách string[] tmp = item.JsonData.Replace("[", "").Replace("]", "").Replace("},{", "#").Replace("{", "") .Replace("}", "").Replace("\"", "").Split('#'); //Chạy để insert và update từng bản ghi một foreach (var jsonRecord in tmp) { string[] colArr = jsonRecord.Split(','); string columnInsert = ""; string valueInsert = ""; string valueUpdate = ""; foreach (var col in colArr) { string[] i = col.Split(':'); //fix lại định dạng ngày tháng, [nên chuyển thành regex để bắt lỗi] if (i[1].Contains("-") && i[1].Length == 13) { i[1] = i[1].Replace("T00", ""); } if (i[0] != frmTmp.PrimaryKey) { valueUpdate += "[" + i[0] + "] = N'" + i[1] + "',"; //Khóa chính mặc định là tự động tăng, nếu muốn ko tự động tăng thì phải tạo thêm 1 cấu hình columnInsert += "[" + i[0] + "],"; valueInsert += "N'" + i[1] + "',"; } else { if (frmTmp.PrKeyIsAutoIncrement == false) { columnInsert += "[" + i[0] + "],"; valueInsert += "N'" + i[1] + "',"; } sqlUpdate += i[1] + "'"; checkSql = "select top 1 * from " + tableName + " where " + frmTmp.PrimaryKey + " = N'" + i[1] + "'"; } } if (DataHandler.GetInstance().ExecuteScalar(checkSql) != null) { valueUpdate += "[" + frmTmp.ForeignKey + "] = N'" + ForeignKeyValue + "'"; //Thêm cột khóa ngoại vào câu lệnh insert sqlUpdate = string.Format(sqlUpdate, valueUpdate, tableName); DataHandler.GetInstance().ExecuteNonQuery(sqlUpdate); } else { columnInsert += "[" + frmTmp.ForeignKey + "]"; valueInsert += "N'" + ForeignKeyValue + "'"; sqlInsert = string.Format(sqlInsert, columnInsert, valueInsert, tableName); DataHandler.GetInstance().ExecuteNonQuery(sqlInsert); } // Dialog.ShowNotification(sqlUpdate + " " + sqlInsert); } } } }