public void CollectData(object objData, ICollection controls, bool isEntityClass) { if (!isEntityClass) { return; } //处理实体对象 Type type = objData.GetType(); object obj = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null); foreach (object control in controls) { if (control is IDataControl) { //情况单独处理 IDataControl brainControl = control as IDataControl; if (brainControl.IsValid) { //string cao = obj.GetType().Name; if (brainControl.LinkObject == obj.GetType().Name&& brainControl.LinkProperty != "") { object oValue = brainControl.GetValue(); //add 2008.7.22 if (brainControl.SysTypeCode != TypeCode.String && (oValue == null || oValue.ToString() == "")) { continue; } //EditFlag 邓太华 2006.9.17 处理 System.DBNull.Value 情况 if (oValue == System.DBNull.Value) { type.InvokeMember(brainControl.LinkProperty, BindingFlags.SetProperty, Type.DefaultBinder, objData, new object[] { null }); continue; } type.InvokeMember(brainControl.LinkProperty, BindingFlags.SetProperty, Type.DefaultBinder, objData, new object[] { Convert.ChangeType(oValue, brainControl.SysTypeCode) }); } } else { throw new Exception("绑定到属性字段[" + brainControl.LinkProperty + "]前未通过服务器验证!"); } } } }
protected bool AutoUpdateIBFormDataInner(List <IBCommand> ibCommandList, IDataControl guidControl) { object guidObj = guidControl.GetValue(); if (guidObj == null || guidObj.ToString() == "") { throw new Exception("GUID 主键或字符型主键列更新数据不能为空!"); } if (guidControl.ReadOnly) { throw new Exception("GUID 主键或字符型主键列更新数据时不能设置为只读!"); } if (!guidControl.PrimaryKey) { throw new Exception("GUID 主键或字符型主键列更新数据时必须设置PrimaryKey属性!"); } string guidText = guidObj.ToString(); int result = 0; foreach (IBCommand command in ibCommandList) { if (command.TableName == guidControl.LinkObject) { string sql = "SELECT [" + guidControl.LinkProperty + "] FROM [" + guidControl.LinkObject + "] WHERE [" + guidControl.LinkProperty + "] = '" + guidText + "'"; object guidInDb = DAO.ExecuteScalar(sql); if (guidInDb != null && guidInDb.ToString() == guidText) { //在数据库中有该记录 result = DAO.ExecuteNonQuery(command.UpdateCommand, CommandType.Text, command.Parameters); } else { result = DAO.ExecuteNonQuery(command.InsertCommand, CommandType.Text, command.Parameters); } return(result > 0); } } return(false); }
private DataRow GetDateTable(string TableName, DataRow dr, ICollection Controls) { foreach (object control in Controls) { if (control is IDataControl) { IDataControl brainControl = control as IDataControl; if (brainControl.LinkObject == TableName) { if (brainControl.Validate() && !brainControl.isNull) { dr[brainControl.LinkProperty] = brainControl.GetValue(); } else { throw new Exception("数据类型错误:" + brainControl.LinkProperty); } } } } return(dr); }
/// <summary> /// 自动更新含有GUID主键的窗体数据,注该GUID控件必须设置PrimaryKey属性 /// </summary> /// <param name="Controls">控件集合</param> /// <param name="guidControl">Gudi控件</param> /// <returns>更新是否成功</returns> public bool AutoUpdateIBFormData(ControlCollection Controls, IDataControl guidControl) { object guidObj=guidControl.GetValue(); if (guidObj == null || guidObj.ToString() == "") throw new Exception("GUID 主键列更新数据不能为空!"); if (guidControl.ReadOnly ) throw new Exception("GUID 主键列更新数据时不能设置为只读!"); if (!guidControl.PrimaryKey ) throw new Exception("GUID 主键列更新数据时必须设置PrimaryKey属性!"); string guidText = guidObj.ToString(); List<IBCommand> ibCommandList = GetIBFormData(Controls); int result = 0; foreach (IBCommand command in ibCommandList) { if (command.TableName == guidControl.LinkObject) { string sql = "select " + guidControl.LinkProperty + " from " + guidControl.LinkObject + " where " + guidControl.LinkProperty + "='" + guidText + "'"; object guidInDb = DAO.ExecuteScalar(sql); if (guidInDb != null && guidInDb.ToString() == guidText) { //在数据库中有该记录 result = DAO.ExecuteNonQuery(command.UpdateCommand); } else { result = DAO.ExecuteNonQuery(command.InsertCommand ); } return result>0 ; } } return false; }
/// <summary> /// 收集控件的查询字符串,例如已经为控件指定了查询条件比较符号。 /// </summary> /// <param name="conlObject">容器对象</param> /// <returns>查询字符串</returns> public static string CollectQueryString(ICollection Controls) { string conditin = string.Empty; foreach (object control in Controls) { if (control is IDataControl && control is IQueryControl) { //((IDataControl)control).SetValue(""); IDataControl ibC = (IDataControl)control; object Value = ibC.GetValue(); //如果控件值为空,那么跳过. if (Value == null || Value.ToString() == "") { continue; } string compareSymbol = ((IQueryControl)ibC).CompareSymbol; string queryFormatString = ((IQueryControl)ibC).QueryFormatString; //默认的比较符号为 等于 "=" if (compareSymbol == "") { compareSymbol = "="; } conditin += " And " + ibC.LinkObject + "." + ibC.LinkProperty + " " + compareSymbol + " "; if (ibC.SysTypeCode == TypeCode.String || ibC.SysTypeCode == TypeCode.DateTime) { string sValue = Value.ToString().Replace("'", "''"); if (queryFormatString != "") { sValue = String.Format(queryFormatString, sValue); conditin += sValue.ToString(); } else { if (compareSymbol.Trim().ToLower() == "like") { conditin += "'%" + sValue + "%'"; } else { conditin += "'" + sValue + "'"; } } } else { conditin += Value.ToString(); } //// //if (tb.QueryFormatString != "") //{ // conditin += String.Format(tb.QueryFormatString, tb.Text.Replace("'", "''")); //} //else //{ // if (tb.SysTypeCode == TypeCode.String) // if (tb.CompareSymbol.Trim().ToLower() == "like") // conditin += "'%" + tb.Text.Replace("'", "''") + "%'"; // else // conditin += "'" + tb.Text.Replace("'", "''") + "'"; // else // conditin += Value.ToString(); //} // //文本框的特殊处理 // if (ibC is CBrainTextBox) // { // CBrainTextBox tb = control as CBrainTextBox; // if (tb.QueryFormatString != "") // { // conditin += String.Format(tb.QueryFormatString, tb.Text.Replace("'", "''")); // } // else // { // if (tb.SysTypeCode == TypeCode.String) // if (tb.CompareSymbol.Trim().ToLower() == "like") // conditin += "'%" + tb.Text.Replace("'", "''") + "%'"; // else // conditin += "'" + tb.Text.Replace("'", "''") + "'"; // else // conditin += Value.ToString(); // } // } // else if (ibC is WelTop.ControlLibrary.Controls.Calendar) // { // //处理日历控件 // WelTop.ControlLibrary.Controls.Calendar tb = control as WelTop.ControlLibrary.Controls.Calendar; // if (tb.QueryFormatString != "") // { // conditin += String.Format(tb.QueryFormatString, tb.Text); // } // else // { // if (tb.SysTypeCode == TypeCode.String || tb.SysTypeCode == TypeCode.DateTime) // conditin += "'" + tb.Text.Replace("'", "''") + "'"; // else // conditin += Value.ToString(); // } // } // else // { // if (ibC.SysTypeCode == TypeCode.String) // conditin += "'" + Value.ToString().Replace("'", "''") + "'"; // else // conditin += Value.ToString(); // } //} //else //{ // conditin += CollectQueryString(control); //} } } return(conditin); }
/// <summary> /// 获取更新表单数据的智能表单命令,以备将这些控件中的数据保存到数据库 /// <remarks> /// 注意:1,主键数据可以插入数据库,但不可直接更新, /// 如果想更新主键字段,需要另外增加一个数据文本框控件,绑定主键字段名,但不设置主键属性, /// 同时放置一个标签数据控件,设置为主键属性并需要运行是赋值给此控件。 /// 2,自增主键字段对应的数据控件,需要设置主键属性并且不可以更新此控件的值到数据库。 /// </remarks> /// </summary> /// <param name="IBControls"></param> /// <param name="DB"></param> /// <returns></returns> protected static List <IBCommand> GetIBFormDataInner(List <IDataControl> IBControls, CommonDB DB) { List <IBCommand> IBCommands = new List <IBCommand>(); //获取表单中的CRUD 语句。 while (IBControls.Count > 0) { string strTableName = ""; string strInsert = ""; string strFields = ""; string strValues = ""; string strUpdate = ""; string strCondition = ""; int nullCount = 0; int ID = -1; int paraIndex = 0; //edit at 2015.3.7 //定义不同的参数列表,修正Access 等数据库根据参数顺序而不是参数名匹配进行查询的问题 List <IDataParameter> paraUpdateList = new List <IDataParameter>(); //更新,删除参数列表 List <IDataParameter> paraInsertList = new List <IDataParameter>(); //插入参数列表 List <IDataParameter> paraPKs = new List <IDataParameter>(); //主键参数列表 for (int i = 0; i < IBControls.Count; i++) // object objCtr in IBControls) { object objCtr = IBControls[i]; if (objCtr != null) { IDataControl ibCtr = objCtr as IDataControl; //只有非只读的控件才可以更新数据到数据库 if (ibCtr != null) { if (strTableName == "" && ibCtr.LinkObject != "") { strTableName = ibCtr.LinkObject; strInsert = "INSERT INTO [" + strTableName + "]("; strUpdate = "UPDATE [" + strTableName + "] SET "; } //找到当前处理的表,只有非只读的字段可以更新 if (strTableName == ibCtr.LinkObject && ibCtr.LinkProperty != "") { #region 原来获取值得方法 //string cValue=ibCtr.GetValue ().ToString ().Replace ("'","''"); ////dth,2008.4.11 处理字符串类型为空的情况 ////防止SQL注入式攻击 ////不论是否为空都进行字符串类型测试 //if(ibCtr.SysTypeCode==System.TypeCode.String || ibCtr.SysTypeCode==System.TypeCode.Empty) //{ // cValue="'"+ cValue +"'"; //} //else //{ // if(cValue!="") // { // if(ibCtr.SysTypeCode==System.TypeCode.Boolean ) // cValue=(cValue.ToUpper ()=="TRUE"?"1":"0"); // else if(ibCtr.SysTypeCode==System.TypeCode.DateTime ) // { // if (DB.CurrentDBMSType == DBMSType.SQLite) // cValue = "'" + DateTime.Parse(cValue).ToString("s") + "'"; // else // cValue = "'" + cValue + "'";//SQL SERVER 日期格式 // } // else if(ibCtr.SysTypeCode==System.TypeCode.DBNull ) // { // cValue="NULL"; // } // else if(ibCtr.SysTypeCode==System.TypeCode.Object ) // { // //Object 标记不做任何处理,例如可以使用最大值加一获取主键值 // } // else if(!(ibCtr.SysTypeCode==System.TypeCode.String || ibCtr.SysTypeCode==System.TypeCode.Empty)) // { // //如果不是字符串那么试图进行数字转换 // cValue=Convert.ToDouble (cValue).ToString (); // } // } //} #endregion string ctlParaName = string.Empty; object ctlValue = ibCtr.GetValue(); //非只读的数据才更新 if (ctlValue != DBNull.Value) { ctlParaName = DB.GetParameterChar + "P" + paraIndex++; IDataParameter para = DB.GetParameter(ctlParaName, ctlValue); if (ibCtr.SysTypeCode == System.TypeCode.String || ibCtr.SysTypeCode == System.TypeCode.Empty) { if (ibCtr is IDataTextBox) { int maxStringLength = ((IDataTextBox)ibCtr).MaxLength; if (maxStringLength > 0) { ((IDbDataParameter)para).Size = maxStringLength; } } } //2010.1.25 取消 ibCtr.PrimaryKey 不能更新的限制,例如可以让GUID主键列可以更新 //2015.3.7 恢复 ibCtr.PrimaryKey 不能更新的限制, //如果需要更新主键,请设置另外一个控件并将它绑定位主键字段但不设置 PrimaryKey 属性 //如果是自增列,设置该列的控件属性为 只读属性即可。 if (ibCtr.PrimaryKey) //只要是主键就作为更新的条件 { if (!string.IsNullOrEmpty(ctlValue.ToString())) { strCondition += " And [" + ibCtr.LinkProperty + "] = " + ctlParaName; if (ibCtr.SysTypeCode == System.TypeCode.Int32) { ID = int.Parse(ctlValue.ToString()); } else { ID = -2;//主键可能是非数字型 } paraPKs.Add(para); //主键数据也可能需要插入 if (!ibCtr.ReadOnly) { strFields += "[" + ibCtr.LinkProperty + "],"; strValues += ctlParaName + ","; paraInsertList.Add(para); } } } else if (!ibCtr.ReadOnly) { strFields += "[" + ibCtr.LinkProperty + "],"; strValues += ctlParaName + ","; strUpdate += "[" + ibCtr.LinkProperty + "] = " + ctlParaName + ","; paraUpdateList.Add(para); paraInsertList.Add(para); } } } IBControls[i] = null; } } else { nullCount++; } }//end for if (nullCount >= IBControls.Count - 1) { break; } strInsert += strFields.TrimEnd(',') + ") VALUES (" + strValues.TrimEnd(',') + ")"; strUpdate = strUpdate.TrimEnd(',') + " WHERE 1=1 " + strCondition; IBCommand ibcmd = new IBCommand(strTableName); ibcmd.InsertCommand = strInsert; ibcmd.UpdateCommand = strUpdate; //if( ID>0) ibcmd.InsertedID = ID; ibcmd.InsertParameters = paraInsertList.ToArray(); paraUpdateList.AddRange(paraPKs); ibcmd.Parameters = paraUpdateList.ToArray(); IBCommands.Add(ibcmd); }//end while return(IBCommands); }
/// <summary> /// 获取选择和删除查询的SQL语句 /// </summary> /// <param name="IBControls">已经收集的控件集合</param> /// <returns> ArrayList 中的成员为 IBCommand 对象,包含具体的CRUD SQL</returns> public static List <IBCommand> GetSelectAndDeleteCommand(List <IDataControl> IBControls) { List <IBCommand> IBCommands = new List <IBCommand>(); //获取表单中的CRUD 语句。 while (IBControls.Count > 0) { string strTableName = ""; string strSelect = ""; string strDelete = ""; string strFields = ""; //string strValues=""; string strCondition = ""; int nullCount = 0; for (int i = 0; i < IBControls.Count; i++)// object objCtr in IBControls) { object objCtr = IBControls[i]; if (objCtr != null) { IDataControl ibCtr = objCtr as IDataControl; //只有非只读的控件才可以更新数据到数据库 if (ibCtr != null) { if (strTableName == "") { strTableName = ibCtr.LinkObject; strSelect = "SELECT "; strDelete = "DELETE FROM [" + strTableName + "]"; } //找到当前处理的表,只读的字段也可以处理 if (strTableName == ibCtr.LinkObject && ibCtr.LinkObject != "") { string cValue = ibCtr.GetValue().ToString().Replace("'", "''"); if (ibCtr.PrimaryKey) { if (cValue != "") { //防止SQL注入式攻击 cValue = (ibCtr.SysTypeCode == System.TypeCode.String || ibCtr.SysTypeCode == System.TypeCode.DateTime ? "'" + cValue + "'" : Convert.ToDouble(cValue).ToString()); } strCondition += " And [" + ibCtr.LinkProperty + "] = " + cValue; } string temp = "[" + ibCtr.LinkProperty + "],"; if (ibCtr.LinkProperty != "" && strFields.IndexOf(temp) == -1) { strFields += temp; } IBControls[i] = null; } if (ibCtr.LinkObject == "" || ibCtr.LinkProperty == "") { IBControls[i] = null; } } } else { nullCount++; } }//end for if (strFields == "") { break; } strSelect += strFields.TrimEnd(',') + " FROM [" + strTableName + "] WHERE 1=1 " + strCondition; strDelete += " WHERE 1=1 " + strCondition; IBCommand ibcmd = new IBCommand(strTableName); ibcmd.SelectCommand = strSelect; ibcmd.DeleteCommand = strDelete; IBCommands.Add(ibcmd); if (nullCount >= IBControls.Count - 1) { break; } }//end while return(IBCommands); }
public void CollectData(object objData, ICollection controls, bool isEntityClass) { //不再需要判断 //if (!isEntityClass) // return; //处理实体对象 if (objData is EntityBase) { EntityBase entity = objData as EntityBase; string typeName = objData.GetType().Name; foreach (object control in controls) { if (control is IDataControl) { //情况单独处理 IDataControl brainControl = control as IDataControl; if (brainControl.IsValid) { if (string.IsNullOrEmpty(brainControl.LinkProperty)) { continue; } if (brainControl.LinkObject == typeName || brainControl.LinkObject == entity.TableName)//实体类的类型名称映射方式或者表方式 { object oValue = brainControl.GetValue(); //不填写非文本内容,不更新? //if (oValue == DBNull.Value) //{ // entity[brainControl.LinkProperty] = oValue; // continue; //} //add 2008.7.22 if (brainControl.SysTypeCode != TypeCode.String && (oValue == null || oValue.ToString() == "")) { continue; } //EditFlag 邓太华 2006.9.17 处理 System.DBNull.Value 情况 if (oValue != System.DBNull.Value) { //支持实体类的属性访问,数据控件上写属性名 edit @ 2014.3.12 try { if (brainControl.LinkObject == typeName)//属性映射房四海 { entity[brainControl.LinkProperty] = oValue; } else // 表映射方式 { entity.setProperty(brainControl.LinkProperty, oValue); } } catch (ArgumentException ex) { //处理非PDF.NET的实体类属性,反射写入值 string linkProp = brainControl.LinkProperty; bool flag = false; foreach (var prop in entity.GetType().GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)) { if (string.Compare(prop.Name, linkProp, true) == 0 && prop.CanWrite) { prop.SetValue(entity, oValue, null); flag = true; break; } } if (!flag) { throw new ArgumentOutOfRangeException("已经进行异常处理,但是属性未找到或者不可写,原始错误信息:" + ex.Message); } } } } } else { throw new Exception("绑定到属性字段[" + brainControl.LinkProperty + "]前未通过服务器验证!"); } } } } else { Type type = objData.GetType(); object obj = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null); foreach (object control in controls) { if (control is IDataControl) { //情况单独处理 IDataControl brainControl = control as IDataControl; if (brainControl.IsValid) { //string cao = obj.GetType().Name; if (brainControl.LinkObject == obj.GetType().Name&& brainControl.LinkProperty != "") { object oValue = brainControl.GetValue(); //add 2008.7.22 if (brainControl.SysTypeCode != TypeCode.String && (oValue == null || oValue.ToString() == "")) { continue; } //EditFlag 邓太华 2006.9.17 处理 System.DBNull.Value 情况 if (oValue == System.DBNull.Value) { type.InvokeMember(brainControl.LinkProperty, BindingFlags.SetProperty, Type.DefaultBinder, objData, new object[] { null }); continue; } if (brainControl.SysTypeCode == TypeCode.Empty) { throw new Exception("收集数据控件的数据失败,SysTypeCode 属性不能为Empty !"); } type.InvokeMember(brainControl.LinkProperty, BindingFlags.SetProperty, Type.DefaultBinder, objData, new object[] { Convert.ChangeType(oValue, brainControl.SysTypeCode) }); } } else { throw new Exception("绑定到属性字段[" + brainControl.LinkProperty + "]前未通过服务器验证!"); } } } } }