/// <summary> /// 将值转换成合适的值字符串 /// 1.Number: 无需转换 /// 2.String: '值' /// 3.Date: TO_DATE('值','YYYY-MM-DD') /// </summary> /// <param name="Value"></param> /// <param name="ValueType"></param> /// <returns></returns> private static string CheckValue(object Value, SearchValueType Type, string Op) { var Result = string.Empty; switch (Type) { case SearchValueType.Number: Result = Value.ToString(); break; case SearchValueType.String: if (Op == "Like") { Result = "'%" + Value.ToString() + "%'"; } else { Result = "'" + Value.ToString() + "'"; } break; case SearchValueType.Date: Result = "TO_DATE('" + Value.ToString() + "','YYYY-MM-DD HH24:MI:SS')"; break; } return(Result); }
/// <summary> /// 构造函数 /// </summary> /// <param name="pFieldName">列名</param> /// <param name="pFieldCaption">列名描述(中文)</param> /// <param name="pOperatorArray"></param> /// <param name="pOperator">运算符</param> /// <param name="pSourceValue"></param> /// <param name="pSourceValueType"></param> /// <param name="pDisplayField"></param> /// <param name="pDisplayFieldType"></param> /// <param name="pDisplayValue"></param> /// <param name="pValueField"></param> /// <param name="pValueFieldType"></param> /// <param name="pValue">查询值</param> /// <param name="pValueType">查询值类型</param> public Condition( string pFieldName, string pFieldCaption, string[] pOperatorArray, string pOperator, object pSourceValue, Type pSourceValueType, string pDisplayField, string pDisplayFieldType, object pDisplayValue, string pValueField, string pValueFieldType, object pValue, SearchValueType pValueType) { IsChoose = false; FieldName = pFieldName; FieldCaption = pFieldCaption; ValueType = pValueType; OperatorArray = pOperatorArray ?? UsualOperatorType(ValueType); if (pValue == null) { throw new ArgumentNullException("pValue"); } Operator = pOperator; SourceValue = pSourceValue; SourceValueType = pSourceValueType; DisplayField = pDisplayField; DisplayFieldType = pDisplayFieldType; DisplayValue = pDisplayValue; ValueField = pValueField; ValueFieldType = pValueFieldType; Value = pValue; }
/// <summary> /// 将值转换成合适的值字符串 /// 1.Number: 无需转换 /// 2.String: '值' /// 3.Date: TO_DATE('值','YYYY-MM-DD') /// </summary> /// <param name="value"></param> /// <param name="type"></param> /// <param name="op"></param> /// <returns></returns> private static string CheckValue(object value, SearchValueType type, string op) { var result = string.Empty; switch (type) { case SearchValueType.Number: result = value.ToString(); break; case SearchValueType.String: if (op == "Like") { result = "'%" + value + "%'"; } else { result = "'" + value + "'"; } break; case SearchValueType.Date: result = "TO_DATE('" + value + "','YYYY-MM-DD HH24:MI:SS')"; break; } return(result); }
/// <summary> /// 构造函数 /// </summary> /// <param name="p_FieldName">列名</param> /// <param name="p_FieldNameNote">列名描述(中文)</param> /// <param name="p_Value">查询值的初始值</param> /// <param name="p_ValueType">查询值的类型</param> public Condition(string p_FieldName, string p_FieldCaption, object p_Value, SearchValueType p_ValueType) { FieldName = p_FieldName; FieldCaption = p_FieldCaption; ValueType = p_ValueType; Value = p_Value; OperatorArray = UsualOperatorType(ValueType); Operator = OperatorArray[0]; }
/// <summary> /// 构造函数 /// </summary> /// <param name="pFieldName">列名</param> /// <param name="pFieldCaption">列名描述(中文)</param> /// <param name="pValue">查询值的初始值</param> /// <param name="pValueType">查询值的类型</param> public Condition(string pFieldName, string pFieldCaption, object pValue, SearchValueType pValueType) { IsChoose = false; FieldName = pFieldName; FieldCaption = pFieldCaption; ValueType = pValueType; Value = pValue; OperatorArray = UsualOperatorType(ValueType); Operator = OperatorArray[0]; }
/// <summary> /// 根据值类型获取常用的操作符 /// 1.数字和日期的常用操作符:"=", ">", "<", ">=", "<=", "<>" /// <param name="valueType">查询值类型</param> /// </summary> private string[] UsualOperatorType(SearchValueType valueType) { switch (valueType) { case SearchValueType.Number: case SearchValueType.Date: var usual = new string[] { "=", ">", "<", ">=", "<=", "<>" }; return(usual); case SearchValueType.String: var strusual = new string[] { "=", ">", "<", ">=", "<=", "<>", "Like" }; return(strusual); } return(null); }
/// <summary> /// 确定按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Confirm_Click(object sender, EventArgs e) { try { this.CondtionList.Clear(); for (int i = 0; i < this.dt_MyDataSet.Rows.Count; i++) { if (Convert.ToBoolean(dt_MyDataSet.Rows[i]["Choose"])) { SearchValueType t = new SearchValueType(); t = SearchValueType.String; if (dt_MyDataSet.Rows[i]["Type"].ToString() == typeof(Decimal).Name) { t = SearchValueType.Number; } if (dt_MyDataSet.Rows[i]["Type"].ToString() == typeof(String).Name) { t = SearchValueType.String; } if (dt_MyDataSet.Rows[i]["Type"].ToString() == typeof(DateTime).Name) { t = SearchValueType.Date; } if (dt_MyDataSet.Rows[i]["Type"].ToString() == typeof(Object).Name) { t = SearchValueType.String; } Condition cond = new Condition(dt_MyDataSet.Rows[i]["ColumnFiledName"].ToString(), dt_MyDataSet.Rows[i]["ColumnName"].ToString(), dt_MyDataSet.Rows[i]["ValueDisplay"].ToString(), t); cond.Operator = ToSqlString(dt_MyDataSet.Rows[i]["Operater"].ToString()); CondtionList.Add(cond); } } this.DialogResult = DialogResult.OK; } catch (Exception ee) { // Program.MsgBoxError(ee); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="p_FieldName">列名</param> /// <param name="p_FieldNameNote">列名描述(中文)</param> /// <param name="p_OperatorType">运算符</param> /// <param name="p_DefaultOperatorIndex">缺省运算符的索引</param> /// <param name="p_Value">查询值</param> /// <param name="p_ValueType">查询值类型</param> public Condition( string p_FieldName, string p_FieldCaption, string[] p_OperatorArray, string p_Operator, object p_SourceValue, Type p_SourceValueType, string p_DisplayField, string p_DisplayFieldType, object p_DisplayValue, string p_ValueField, string p_ValueFieldType, object p_Value, SearchValueType p_ValueType) { FieldName = p_FieldName; FieldCaption = p_FieldCaption; ValueType = p_ValueType; if (p_OperatorArray != null) { OperatorArray = p_OperatorArray; } else { OperatorArray = UsualOperatorType(ValueType); } Operator = p_Operator; SourceValue = p_SourceValue; SourceValueType = p_SourceValueType; DisplayField = p_DisplayField; DisplayFieldType = p_DisplayFieldType; DisplayValue = p_DisplayValue; ValueField = p_ValueField; ValueFieldType = p_ValueFieldType; Value = p_Value; }