动态添加SqlParameter
Dictionary<string, object> mDic = new Dictionary<string, object>(); if (!string.IsNullOrEmpty(MerchantName)) { mDic.Add("@MerchantName", "%" + MerchantName + "%" ); } foreach (var item in mDic) { ilist.Add(new SqlParameter(item.Key, item.Value)); } foreach (var item in ilist) { if (item.ToString()== "@MerchantName") { cmdText += "AND a.UserName LIKE "+item+""; } }