public int SaveToDataRow(XModel updateModel, DataRow dataRowToUpdate) { if (dataRowToUpdate != null) { XModel xdm = updateModel; for (int i = 0; i < xdm.Fields.Count; i++) { string fname = xdm.Fields[i].name; if (dataRowToUpdate.Table.Columns.Contains(fname)) { object o = xdm.Fields[i].value; if (o == null) { o = Convert.DBNull; } dataRowToUpdate[fname] = o; } } return(1); } else { return(0); } }
public int LoadFromDataTable(XModel outputModel, DataTable dtCache, params object[] keyValues) { _CheckKeyField(); if (dtCache == null || dtCache.Rows.Count == 0) { return(-2); } string sCon = null; if (keyValues != null) { LoadKeyValues(outputModel, keyValues); sCon = GetKeyConditionString(false, keyValues); } else { sCon = GetKeyConditionString(false, outputModel); } DataTable dt = dtCache; DataRow[] drs = dtCache.Select(sCon); if (drs.Length > 0) { this.LoadFromDataRow(outputModel, drs[0]); return(drs.Length); } else { return(-1); } }
public int LoadFromDB(XModel outputModel, params object[] keyValues) { _CheckKeyField(); string sCon = null; if (keyValues != null) { LoadKeyValues(outputModel, keyValues); sCon = GetKeyConditionString(true, keyValues); } else { sCon = GetKeyConditionString(true, outputModel); } string sSql = "Select * from " + this.ViewName + sCon; DataTable dt = this.GetDataTable(sSql); if (dt != null && dt.Rows.Count != 0) { DataRow dr = dt.Rows[0]; this.LoadFromDataRow(outputModel, dr); return(1); } else { return(0); } }
public int Insert(XModel insertModel, bool returnIdentityID) { string sSql = this.InsertSql(insertModel); int iR; if (string.IsNullOrEmpty(sSql)) { return(-1); } if (returnIdentityID) { sSql = sSql + ";SELECT @@IDENTITY"; } if (!returnIdentityID) { iR = this.Execute(sSql); } else { iR = Convert.ToInt32(this.GetValue(sSql)); } if (iR > 0) { insertModel.ClearAllUpdated(); } return(iR); }
public int UpdateOne(XModel setsModel) { string sSql = UpdateOneSql(setsModel); if (String.IsNullOrEmpty(sSql)) { if (System.Configuration.ConfigurationManager.AppSettings["ReturnValueWhenUpdateEmpty"] != null) { return(Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ReturnValueWhenUpdateEmpty"])); } else { return(0); } } else { int iR = this.Execute(sSql); if (iR == 1) { setsModel.ClearAllUpdated(); } return(iR); } }
public string GetKeyConditionString(bool addWhere, XModel xm) { StringBuilder sb = new StringBuilder(); String[] keyFields = KeyFields; for (int i = 0; i < keyFields.Length; i++) { string kf = keyFields[i]; //object kv = xm.DFields[kf].ValueStr(); if (sb.Length != 0) { sb.Append(" and "); } sb.Append(xm.DFields[kf].Condition); //if (kv == Convert.DBNull) // sb.AppendFormat("{0} IS NULL", kf); //else if (xm.DFields[kf].type == DbType.Guid) //{ // //sb.AppendFormat("{0} = {1}", kf, ULCode.XSql.Sql.GetQueryGuidFieldValue(this.CnName, xm.DFields[kf].value)); // sb.AppendFormat("{0} = '{1}'", kf, kv); //} //else // sb.AppendFormat("{0} = '{1}'", kf, kv); } if (sb.Length != 0 && addWhere) { sb.Insert(0, " where "); } return(sb.ToString()); }
// public void LoadKeyValues(XModel outputModel, params object[] keyValues) { for (int i = 0; i < KeyFields.Length; i++) { string key = KeyFields[i]; object value = keyValues[i]; outputModel.DFields[key].set(value); } }
public void ExportInto(XModel otherModel) { List <XDataField> thisFields = this.Fields; Dictionary <string, XDataField> otherFields = otherModel.DFields; foreach (XDataField xdf in thisFields) { if (otherFields.ContainsKey(xdf.name)) { otherFields[xdf.name].set(xdf.value); } } }
public string UpdateOneSql(XModel setsModel) { _CheckKeyField(); string sets = setsModel.SetsListString(); if (string.IsNullOrEmpty(sets)) { return(null); } else { return("Update " + this.TableName + " set " + sets + GetKeyConditionString(true, setsModel)); } }
public string InsertSql(XModel insertModel) { string f1 = insertModel.FieldsListStringForInsert(false); string f2 = insertModel.ValuesListStringForInsert(false); if (String.IsNullOrEmpty(f1) || String.IsNullOrEmpty(f2)) { return(null); } else { return("Insert into " + this.TableName + "(" + f1 + ") values(" + f2 + ")"); } }
///************************************************************** ///outputModel ///关键值未赋,称为状态A=未初始化状态; ///关键值已赋,但其它值未赋,称为状态B=已初始化状态; ///所有值已经赋为为C=完整状态 ///当keyValues为空时,则outputModel不能为未初始化状态。 public void LoadFromDataRow(XModel outputModel, DataRow drCache) { if (drCache != null) { XModel xdm = outputModel; for (int i = 0; i < xdm.Fields.Count; i++) { string fname = xdm.Fields[i].name; if (drCache.Table.Columns.Contains(fname)) { xdm.Fields[i].value = drCache[fname]; } } xdm.LoadSucceed = true; outputModel.ClearAllUpdated(); } }
public int SaveToDataTable(XModel updateModel, DataTable dataTableToUpdate) { if (dataTableToUpdate != null) { DataRow dr; DataRow[] drs = dataTableToUpdate.Select(GetKeyConditionString(false, updateModel)); if (drs.Length == 0) { dr = dataTableToUpdate.Rows.Add(new object[0]); } else { dr = drs[0]; } return(this.SaveToDataRow(updateModel, dr)); } else { return(0); } }
///************************************************************** public int SaveToDb(XModel updateModel) { _CheckKeyField(); string sets = updateModel.SetsListString(); string fieldLists = updateModel.FieldsListStringForInsert(false); string valueLists = updateModel.ValuesListStringForInsert(false); //string whereStr=updateModel.DefaultConditionString(this.KeyField); string whereStr = GetKeyConditionString(false, updateModel); //if (string.IsNullOrEmpty(sets)) //{ // return 0; //} string sSql_Exists = "Select * from " + this.TableName + " where " + whereStr; string sSql_Update = String.IsNullOrEmpty(sets) ? "Select 1;" : "Update " + this.TableName + " set " + sets + " where " + whereStr + ";"; string sSql_Insert = String.IsNullOrEmpty(fieldLists) || String.IsNullOrEmpty(valueLists) ? "Select 1;" : "Insert into " + this.TableName + "(" + fieldLists + ") values(" + valueLists + ");"; string sSql = String.Format("if exists({0}) {1} else {2}", sSql_Exists, sSql_Update, sSql_Insert); int iR = 0; ULCode.QDA.ProviderType ct = XSql.GetDB(this.CnName).GetProviderType(); if (ct == ProviderType.MsSql) { iR = this.Execute(sSql); } else// if (ct == ProviderType.OleDb) { if (XSql.IsHasRow(this.CnName, sSql_Exists)) { iR = this.Execute(sSql_Update); } else { iR = this.Execute(sSql_Insert); } } if (iR > 0) { updateModel.ClearAllUpdated(); } return(iR); }
///************************************************************** public int Insert(XModel insertModel) { return(Insert(insertModel, false)); }
public int DeleteOne(XModel xm) { string sSql = "Delete from " + this.TableName + " " + GetKeyConditionString(true, xm); return(this.Execute(sSql)); }