public static SqlQuery ToQuery2(this string sql, dynamic parameters) { SqlQuery q = new SqlQuery(); if (null != parameters) { foreach (PropertyInfo pi in parameters.GetType().GetProperties()) { bool flag = false; object value = pi.GetValue(parameters); if (null != value) { Type valueType = value.GetType(); if (!collTtpes.Contains(valueType)) { IEnumerable list = value as IEnumerable; if (null != list) { char prefix = sql.Contains("@") ? '@' : ':'; StringBuilder sb = new StringBuilder("("); int index = 0; foreach (var item in list) { sb.Append(prefix) .Append(pi.Name + index) .Append(','); q.Parameters.Add(pi.Name + index, item); ++index; } sb.Remove(sb.Length - 1, 1); sb.Append(')'); sql = sql.Replace(prefix + pi.Name, sb.ToString()); flag = true; } } } if (!flag) q.Parameter(pi.Name, value); } } q.Sql(sql); return q; }
//i.e. "update Perseon set Name=@0, No=@1 where Id=@2".ToQuery("Memo", 12, 1); public static SqlQuery ToQuery(this string sql, params object[] parameters) { SqlQuery query = new SqlQuery(); query.Sql(sql, parameters); return query; }