/****************************** * 说明:程序所有特殊规则替换类 * 创建人:龚宇超 * 创建日期:2017-08-23 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Lskj.Model; using System.Data; using System.Data.SqlClient; using DevExpress.XtraTreeList.Nodes; namespace Lskj.Util { /// /// 程序所有特殊规则替换类 /// public static class ReplaceHelper { /// /// 带参数控件替换规则 /// private static readonly string _replace_param = "#[^##]+#"; private static readonly string _replace_parent_left_key = "{#"; private static readonly string _replace_param_first_key = "{"; private static readonly string _replace_param_end_key = "}"; private static readonly string _proc_first_key = "@"; /// /// 树节点替换key /// private static readonly string _replace_parent_key = "{parent.key}"; /// /// 替换sql语句中的对应字段 /// private static readonly string _replace_field_key = "{([^{])+}"; /// /// 存储过程标识 /// private static readonly string _procedureFlag = "exec"; /// /// 查询语句标记 /// private static readonly string _selectFlag = "select"; /// /// 查询条件替换值 /// private static readonly string _search_control_value = "{value}"; /// /// loginid 固定字段 /// private static readonly string _loginid_key = "{loginid}"; /// /// loginname 固定字段 /// private static readonly string _loginname_key = "{loginname}"; /// /// groupid 固定字段 /// private static readonly string _groupid_key = "{groupid}"; /// /// password 固定字段 /// private static readonly string _password_key = "{password}"; /// /// inpassword 固定字段 /// private static readonly string _inpassword_key = "{inpassword}"; /// /// 车间固定字段 /// private static readonly string _cj_key = "{logincj}"; /// /// 机台固定字段 /// private static readonly string _jt_key = "{loginjt}"; /// /// 班次固定字段 /// private static readonly string _bz_key = "{loginbz}"; /// /// 班组固定字段 /// private static readonly string _className_key = "{className}"; /// /// 替换表格列标识 /// private static readonly string _column_key = "{COLUMN_NAME}"; /// /// 替换表格列标题标识 /// private static readonly string _column_title_key = "{COLUMN_TITLE}"; /// /// 取单元格值 /// private static readonly string _column_value_key = "{COLUMN_VALUE}"; /// /// 系统id固定字段 /// private static readonly string _seriesId_id = "{SeriesId}"; /// /// 系统种类固定字段 /// private static readonly string _logintype = "{logintype}"; /// /// UserCode 固定字段 /// private static readonly string _userCode_key = "{lserpUserCode}"; /// /// P_EmployeeTab表LoginAccount 固定字段(UserCode) /// private static readonly string _loginaccount_key = "{loginaccount}"; /// /// 账套 固定字段 /// private static readonly string _accountBook = "{lserpAccountBook}"; /// /// 登录人联系电话 /// private static readonly string _loginUserLinkPhone_key = "{loginUserLinkPhone}"; /// /// Mrp标记 /// private static readonly string _mrpTag_key = "{MrpTag}"; /// /// Mrp标记 /// private static readonly string _rightLineStatus = "{RightLineStatus}"; /// /// Mac地址 /// private static readonly string _macAddressy = "{loginMac}"; /// /// 参数替换父级字符前缀 /// /// The replace parent left key. public static string ReplaceParentLeftKey { get { return _replace_parent_left_key; } } /// /// 参数替换首字符 /// /// The replace parameter first key. public static string ReplaceParamFirstKey { get { return _replace_param_first_key; } } /// /// 参数替换尾字符 /// /// The replace parameter end key. public static string ReplaceParamEndKey { get { return _replace_param_end_key; } } /// /// 存储过程参数执行方式首字符 /// /// The proc first key. public static string ProcFirstKey { get { return _proc_first_key; } } /// /// 带参数控件替换规则 /// /// The replace parameter. public static string ReplaceParamKey { get { return _replace_param; } } /// /// 说明:是否为存储过程 /// 创建人:龚宇超 /// 创建日期:2017-09-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// true if the specified default value is procedure; otherwise, false. public static bool IsProcedure(string defaultValue) { string cleanedText = Regex.Replace(defaultValue, @"^(\s*--.*?(\r\n|\n)|\s*)*", ""); return cleanedText.StartsWith("exec", StringComparison.OrdinalIgnoreCase); //return defaultValue.ToLower().Trim().Trim(new char[] { '\r', '\t', '\n' }).StartsWith(_procedureFlag); } /// /// 说明:是否为查询条件 /// 创建人:龚宇超 /// 创建日期:2017-09-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// true if the specified default value is select; otherwise, false. public static bool IsSelect(string defaultValue) { //return defaultValue.ToLower().Trim().Trim(new char[] { '\r', '\t', '\n' }).StartsWith(_selectFlag); //2026-4-17 之前判断不了开头有注释的情况 if (string.IsNullOrWhiteSpace(defaultValue))return false; // 清理:开头所有空白 + 所有单行注释 + 所有多行注释 string cleaned = Regex.Replace( defaultValue, @"^\s*(/\*.*?\*/|\s|--.*?$)*", "", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline ); return cleaned.StartsWith("select", StringComparison.OrdinalIgnoreCase); } /// /// 说明:替换userid、username、groupid /// 创建人:龚宇超 /// 创建日期:2017-10-25 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// System.String. public static string ReplaceUserInfo(string defaultValue) { return ReplaceUserInfo(defaultValue, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, ERPInfo.Instance.GroupId, ERPInfo.Instance.Password); } /// /// 说明:替换userid、username、groupid /// 创建人:龚宇超 /// 创建日期:2017-08-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// The user identifier. /// Name of the user. /// The group identifier. /// System.String. public static string ReplaceUserInfo(string defaultValue, string userId, string userName, string groupId, string password) { if (string.IsNullOrEmpty(defaultValue)) { return defaultValue; } if (defaultValue.IndexOf('{') < 0) { return defaultValue; } return defaultValue = defaultValue.Replace(_loginid_key, userId, true) .Replace(_loginname_key, userName, true) .Replace(_groupid_key, groupId, true) .Replace(_password_key, password, true) .Replace(_inpassword_key, ERPInfo.Instance.InPassWord, true) .Replace(_cj_key, ERPInfo.Instance.WorkCJ + "", true) .Replace(_jt_key, ERPInfo.Instance.WorkJT + "", true) .Replace(_bz_key, ERPInfo.Instance.WorkBZ + "", true) .Replace(_seriesId_id, ERPInfo.Instance.SeriesId + "", true) .Replace(_className_key, ERPInfo.Instance.ClassName + "", true) .Replace(_logintype, "0", true) .Replace(_userCode_key, ERPInfo.Instance.LoginAccount + "", true) .Replace(_loginaccount_key, ERPInfo.Instance.LoginAccount + "", true) .Replace(_accountBook, ERPInfo.Instance.AccountBook + "", true) .Replace(_loginUserLinkPhone_key, ERPInfo.Instance.UserLinkPhone + "", true) .Replace(_mrpTag_key, ERPInfo.Instance.MrpTag + "", true); //.Replace(_macAddressy, ERPInfo.Instance.MacAddress + "", true); } /// /// 说明:带参数替换为1=1,根据#数量替换, /// 创建人:龚宇超 /// 创建日期:2017-08-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.String. public static string ReplaceParam(string defaultValue) { try { if(defaultValue.StartsWith("--不替换")) return defaultValue; if (defaultValue.Contains("#")) { defaultValue = defaultValue.Replace("'#'", "%^&*"); int frequency = SubstringCount(defaultValue, "#"); if (frequency > 1) { if (frequency % 2 == 0) { //#出现次数为偶数 for (int i = 0; i < frequency / 2; i++) { int one = defaultValue.IndexOf("#"); int two = defaultValue.IndexOf("#", one + 1, defaultValue.Length - one - 1); defaultValue = defaultValue.Replace(defaultValue.Substring(one, two + 1 - one), " 1=1 "); } defaultValue = defaultValue.Replace("%^&*", "'#'"); return defaultValue; } else { //#出现次数为奇数 defaultValue = defaultValue.Replace(defaultValue.Substring(defaultValue.IndexOf('#'), defaultValue.LastIndexOf('#') + 1 - defaultValue.IndexOf('#')), " 1=1 "); defaultValue = defaultValue.Replace("%^&*", "'#'"); return defaultValue; } } else { defaultValue = defaultValue.Replace("%^&*", "'#'"); return defaultValue; } } else { return defaultValue; } } catch (Exception) { return Regex.Replace(defaultValue, _replace_param, " 1=1 "); } } public static string ReplaceParamGetTableStructure(string defaultValue) { try { if (SubstringCount(defaultValue, "#") > 1) { return defaultValue.Replace(defaultValue.Substring(defaultValue.IndexOf('#'), defaultValue.LastIndexOf('#') + 1 - defaultValue.IndexOf('#')), " 1<>1 "); } else { //if (defaultValue.IndexOf("order by", StringComparison.OrdinalIgnoreCase) != -1) //{ // int indexOrder = defaultValue.IndexOf("order by", StringComparison.OrdinalIgnoreCase); // string sOrder = string.Empty; // if (indexOrder != -1) // { // sOrder = defaultValue.Substring(indexOrder, defaultValue.Length - indexOrder); // defaultValue = "select * from (" + defaultValue.Substring(0, indexOrder - 1) + ") as t where 1<>1"+ sOrder; // } //} //else //{ // defaultValue = string.Format("select * from ( {0} ) a where 1<>1", defaultValue); //} return string.Format("select * from ( {0} ) a where 1<>1", defaultValue); } } catch (Exception) { return Regex.Replace(defaultValue, _replace_param, " 1<>1 "); } } /// /// 说明:参数替换P_其它参数功能 /// 创建人:王一帆 /// 创建日期:2021-06-08 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.String. public static string ReplaceOtherParam(string defaultValue) { if (defaultValue.ToLower().Contains("{#p_")) { try { List paramList = ReplaceHelper.GetParamFields(defaultValue); foreach (var item in paramList) { string index = item.Replace("{#p_", "").Replace("}", ""); int i = 0; if (int.TryParse(index, out i)) { defaultValue = string.Empty; } } return defaultValue; } catch (Exception) { return Regex.Replace(defaultValue, _replace_param, " 1=1 "); } } else { return defaultValue; } } /// /// 说明:判断字符串出现次数 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The string. /// The substring. /// System.Int32. public static int SubstringCount(string str, string substring) { if (str.Contains(substring)) { string strReplaced = str.Replace(substring, ""); return (str.Length - strReplaced.Length) / substring.Length; } return 0; } /// /// 说明:检查是否包含where条件 /// 创建人:龚宇超 /// 创建日期:2017-09-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The SQL. /// System.String. public static string ReplaceWhereCond(string sql) { if (string.IsNullOrEmpty(sql)) return sql; int w_index = sql.LastIndexOf("where", StringComparison.OrdinalIgnoreCase); // where 后面包含join..on语句,包含则增加一个where 1=1 int j_index = sql.LastIndexOf("join ", StringComparison.OrdinalIgnoreCase); int k_index = sql.LastIndexOf("on ", StringComparison.OrdinalIgnoreCase); int e_index = sql.IndexOf("exec ", StringComparison.OrdinalIgnoreCase); if (e_index == -1) { if (w_index == -1 || (w_index != -1 && w_index < k_index && w_index < j_index && j_index < k_index)) { return sql += " where 1=1 "; } } return sql; } /// /// 说明:替换"{字段}"格式字符串并执行计算,已替换操作员等信息 /// 创建人:龚宇超 /// 创建日期:2017-10-27 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The row. /// The value. /// System.String. public static bool ReplaceRowParamCond(DataRow row, string cond) { if (row == null || string.IsNullOrEmpty(cond)) return true; return EvalCond(ReplaceRowParam(row, cond)); } /// /// 说明:执行条件 /// 创建人:龚宇超 /// 创建日期:2018-03-07 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The cond. /// true if XXXX, false otherwise. public static bool EvalCond(string cond) { //cond = EvalHelper.Eval(ReplaceEvalCond(cond)) + ""; //bool outValue = true; //bool isCond = Boolean.TryParse(cond, out outValue); //return string.IsNullOrEmpty(cond) || !isCond ? true : outValue; try { return string.IsNullOrEmpty(cond) ? true : Convert.ToBoolean(EvalHelper.Eval(ReplaceEvalCond(cond))); } catch (Exception ex) { throw new Exception($"条件判断错误:{cond},{ex.Message}"); } } /// /// 说明:说明:替换"{字段}"格式字符串,已替换操作员等信息 /// 创建人:龚宇超 /// 创建日期:2017-10-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The row. /// The parameter. /// System.String. public static string ReplaceRowParam(DataRow row, string param) { param = ReplaceUserInfo(param); if (row == null) return param; param = param.Replace(_rightLineStatus, (int)row.RowState + "", true); List condition = GetParamFields(param); foreach (string item in condition) { string field = item.Replace("{", "").Replace("}", ""); string value = row.Table.Columns.Contains(field) ? row[field] + "" : item; param = param.Replace(item, value); } return param; } /// /// 说明:说明:替换"{字段}"格式字符串,已替换操作员等信息,其中替换后空值会加上引号 /// {xxx}=0 原本替换 =0 现在替换 ''=0 /// /// /// /// public static string ReplaceRowParamEmptyWrapQuote(DataRow row, string param) { param = ReplaceUserInfo(param); if (row == null) return param; param = param.Replace(_rightLineStatus, (int)row.RowState + "", true); List condition = GetParamFields(param); foreach (string item in condition) { string field = item.Replace("{", "").Replace("}", ""); string value = row.Table.Columns.Contains(field) ? row[field] + "" : item; if (!string.IsNullOrEmpty(value) || value == item) { param = param.Replace(item, value); continue; } string sourceParam = param; param = Regex.Replace(sourceParam, Regex.Escape(item), match => { int rightIndex = match.Index + match.Length; bool isWrappedBySingleQuote = match.Index > 0 && rightIndex < sourceParam.Length && sourceParam[match.Index - 1] == '\'' && sourceParam[rightIndex] == '\''; bool isWrappedByDoubleQuote = match.Index > 0 && rightIndex < sourceParam.Length && sourceParam[match.Index - 1] == '"' && sourceParam[rightIndex] == '"'; return isWrappedBySingleQuote || isWrappedByDoubleQuote ? string.Empty : "''"; }); } return param; } /// /// 说明:说明:替换"{字段}"格式字符串,已替换操作员等信息 /// 创建人:龚宇超 /// 创建日期:2019-07-25 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The row. /// The parameter. /// System.String. public static string ReplaceNodeParam(TreeListNode row, string param) { param = ReplaceUserInfo(param); if (row == null) return param; List condition = GetParamFields(param); foreach (string item in condition) { string field = item.Replace("{", "").Replace("}", ""); string value = row.GetValue(field) + ""; param = param.Replace(item, value); } return param; } /// /// 说明:说明:替换"{字段}"格式字符串,已替换操作员等信息 /// 创建人:龚宇超 /// 创建日期:2018-05-31 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The rows. /// The parameter. /// System.String. public static string ReplaceRowParam(DataRow[] rows, string param, string start = "", string end = "") { if (rows == null || rows.Length == 0) return param; param = ReplaceUserInfo(param); string fieldValues = string.Empty; List fields = ReplaceHelper.GetParamFields(param); foreach (string field in fields) { fieldValues = string.Empty; foreach (DataRow item in rows) { string key = field.Replace("{", "").Replace("}", ""); if (item.Table.Columns.Contains(key) && !string.IsNullOrWhiteSpace(item[key] + "")) fieldValues += start + item[key] + end + ","; } param = param.Replace(field, fieldValues.TrimEnd(',')); } return param; } public static string ReplaceRowParamSplit(DataRow[] rows, string param, string start = "", string end = "", char splitChar = ',') { if (rows == null || rows.Length == 0) return param; param = ReplaceUserInfo(param); string fieldValues = string.Empty; List fields = ReplaceHelper.GetParamFields(param); foreach (string field in fields) { fieldValues = string.Empty; foreach (DataRow item in rows) { string key = field.Replace("{", "").Replace("}", ""); if (item.Table.Columns.Contains(key)) fieldValues += start + item[key] + end + splitChar; } param = param.Replace(field, fieldValues.TrimEnd(splitChar)); } return param; } /// /// 说明:替换"{字段}"格式字符串,已替换操作员等信息,返回SqlParameter集合 /// 创建人:龚宇超 /// 创建日期:2018-01-26 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The row. /// The parameter. /// List<SqlParameter>. public static List ReplaceRowParamToSqlParameter(DataRow row, ref string param) { List paramFields = ReplaceHelper.GetParamFields(param); // 将sql语句组装成Parameter方式,兼容配置语句. List paramList = new List(); if (paramFields != null && paramFields.Count > 0) { foreach (string item in paramFields) { string fieldName = item.Replace(_replace_param_first_key, "").Replace(_replace_param_end_key, ""); string fieldValue = string.Empty; string fieldParam = _proc_first_key + fieldName; if (item.Equals(_loginid_key, StringComparison.OrdinalIgnoreCase)) fieldValue = ERPInfo.Instance.UserId; else if (item.Equals(_loginname_key, StringComparison.OrdinalIgnoreCase)) fieldValue = ERPInfo.Instance.UserName; else if (item.Equals(_groupid_key, StringComparison.OrdinalIgnoreCase)) fieldValue = ERPInfo.Instance.GroupId; param = param.Replace("'" + item + "'", fieldParam).Replace(item, fieldParam); paramList.Add(new SqlParameter(fieldParam, string.IsNullOrEmpty(fieldValue) ? row[fieldName] + "" : fieldValue)); } } return paramList; } /// /// 说明:将条件替换为可执行的条件 /// 创建人:龚宇超 /// 创建日期:2017-10-27 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The cond. /// System.String. public static string ReplaceEvalCond(string cond) { if (!string.IsNullOrEmpty(cond)) { return cond.Replace("=", "==") .Replace("<>", "!=") .Replace("and", "&&") .Replace("or", "||") .Replace(">==", ">=") .Replace("<==", "<=") .Replace(",", ""); } return cond; } /// /// 说明:替换树节点parentkey /// 创建人:龚宇超 /// 创建日期:2017-09-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.String. public static string ReplaceTreeViewParentKeyCond(string defaultValue, string value) { if (string.IsNullOrWhiteSpace(defaultValue)) return defaultValue; //return defaultValue.Replace(_replace_parent_key, string.IsNullOrWhiteSpace(value) ? value : value.Replace("'", "''")); return defaultValue.Replace(_replace_parent_key, value); } /// /// 说明:将{xxx}格式全部替换为某个值 /// 创建人:龚宇超 /// 创建日期:2017-12-01 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// l /// The default value. /// The value. /// System.String. public static string ReplaceParamToValue(string defaultValue, string value) { if (string.IsNullOrEmpty(defaultValue)) return defaultValue; var fields = GetParamFields(defaultValue); foreach (string item in fields) { defaultValue = defaultValue.Replace(item, value); } return defaultValue; } /// /// 说明:将指定{xxx}格式全部替换为某个值,忽略大小写 /// 创建人:龚宇超 /// 创建日期:2018-03-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// The value. /// System.String. public static string ReplaceParamToValue(string defaultValue, string paramStr, string value) { if (string.IsNullOrEmpty(defaultValue) || string.IsNullOrEmpty(paramStr)) return defaultValue; var fields = GetParamFields(defaultValue); foreach (string item in fields) { if (item.Equals(paramStr,StringComparison.OrdinalIgnoreCase)) { defaultValue = defaultValue.Replace(item, value); } } return defaultValue; } /// /// 说明:替换查询条件控件值 /// 创建人:龚宇超 /// 创建日期:2017-09-05 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// The value. /// System.String. public static string ReplaceSearchControlCond(string defaultValue, string value) { if (string.IsNullOrWhiteSpace(defaultValue)) return defaultValue; return defaultValue.Replace(_search_control_value, value); } /// /// 说明:替换表格列(替换column_name、column_title) /// 创建人:龚宇超 /// 创建日期:2018-04-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The default value. /// Name of the column. /// The column caption. /// System.String. public static string ReplaceColumnParam(this string defaultValue, string columnName, string columnCaption, string columnValue) { return defaultValue.Replace(_column_key, columnName).Replace(_column_title_key, columnCaption).Replace(_column_value_key, columnValue); } /// /// 说明:获取sql语句中替换 /// 创建人:龚宇超 /// 创建日期:2017-09-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// List<System.String>. public static List GetParamFields(string defaultValue) { List list = new List(); Regex regex = new Regex(_replace_field_key); MatchCollection mcs = regex.Matches(defaultValue); foreach (Match item in mcs) { if (list.IndexOf(item.Value) == -1) list.Add(item.Value); } return list; } /// /// i人事 许可信息 /// private static readonly string _iPersonnelAllowInformation = "{iPersonnelAllowInformation}"; /// /// i人事 客户唯一身份标识 /// private static readonly string _iPersonnelCustomerId = "{iPersonnelCustomerId}"; /// /// i人事 私钥 /// private static readonly string _iPersonnelPrivateKey = "{iPersonnelPrivateKey}"; /// /// i人事 加密的签名信息 /// private static readonly string _iPersonnelEncryptedMessage = "{iPersonnelEncryptedMessage}"; /// /// 说明:替换i人事接口中的参数 /// 创建人:龚宇超 /// 创建日期:2017-09-04 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// url地址 /// 许可信息 /// 客户标识 /// 私钥 /// System.String. public static string ReplaceIPersonnel(string defaultValue, string iPersonnelAllowInformation, string iPersonnelCustomerId, string iPersonnelPrivateKey) { if (defaultValue.Contains(_iPersonnelAllowInformation) || defaultValue.Contains(_iPersonnelEncryptedMessage)) { //替换许可 defaultValue = defaultValue.Replace(_iPersonnelAllowInformation, iPersonnelAllowInformation); //替换加密的签名信息 TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1); long time = (long)ts.TotalMilliseconds;//当前时间的时间戳 string EncryptedMessage = string.Format("company_id={0}&mobile_no={1}×tamp={2}", iPersonnelCustomerId, ERPInfo.Instance.UserLinkPhone, time); string value = Lskj.Util.EncryptByPrivateKey.encryptByPrivateKey(EncryptedMessage, iPersonnelPrivateKey); defaultValue = defaultValue.Replace(_iPersonnelEncryptedMessage, value); } return defaultValue; } /// /// 替换多行参数 /// [!xxx] 替换为 'xxx','xxx' /// [@xxx] 替换为 'xxx,xxx' /// /// 行 /// 替换值 /// public static string ReplaceMultipleLinesParam(DataRow[] rows, string param) { if (rows == null || rows.Length == 0) return param; param = ReplaceUserInfo(param); string fieldValues = string.Empty; //获取所有[xxx]值,只处理[!xxx]和[@xxx]的值 List fields = new List(); Regex regex = new Regex(@"\[([^\]]+)\]"); MatchCollection mcs = regex.Matches(param); foreach (Match item in mcs) { if (fields.IndexOf(item.Value) == -1) fields.Add(item.Value); } string start = string.Empty; string end = string.Empty; foreach (string field in fields) { fieldValues = string.Empty; start = string.Empty; end = string.Empty; string key = field.Replace("[", "").Replace("]", ""); if (key.StartsWith("!")) { key = key.Replace("!", ""); start = end = "'"; } else if (key.StartsWith("@")) { key = key.Replace("@", ""); start = end = ""; } else { continue; } foreach (DataRow item in rows) { if (item.Table.Columns.Contains(key) && !string.IsNullOrWhiteSpace(item[key] + "")) fieldValues += start + item[key] + end + ","; } param = param.Replace(field, fieldValues.TrimEnd(',')); } return param; } } }