public static void ConfigureFindCommand(ObjectDef m, string schemaName, SqlCommand cmd, params object[] keys) { cmd.CommandType = CommandType.Text; string entityName = m.EntityName; StringBuilder sb = new StringBuilder(); sb.AppendLine("SELECT "); var q = m.GetOrderedProperties(); for (int i = 0; i < q.Count(); i++) { ObjectDefProperty mp = q.ElementAt(i); var entityPropName = mp.ColumnName; if (i == 0) { sb.AppendLine(string.Format("\t[{0}]", entityPropName)); } else { sb.AppendLine(string.Format("\t, [{0}]", entityPropName)); } } sb.AppendLine(string.Format("FROM [{0}].[{1}]", schemaName, entityName)); var qKeys = m.GetKeys().Select(p => p.ColumnName); for (int i = 0; i < qKeys.Count(); i++) { if (keys[i] != null) { sb.AppendLine(string.Format("WHERE [{0}] = @{0}", qKeys.ElementAt(i))); cmd.Parameters.AddWithValue("@" + qKeys.ElementAt(i), keys[i]); } } cmd.CommandText = sb.ToString(); }
public static void ConfigureCountCommand(ObjectDef m, string schemaName, SqlCommand cmd, Condition cond) { cmd.CommandType = CommandType.Text; string entityName = m.EntityName; StringBuilder sb = new StringBuilder(); var strKeys = m.GetKeys().Select(p => p.ColumnName).Aggregate((r, i) => r + ", " + i); sb.AppendLine(string.Format("SELECT COUNT({0})", strKeys)); sb.AppendLine(string.Format("FROM [{0}].[{1}]", schemaName, entityName)); if (cond != null) { string condString = cond.ToSqlString(true); if (!string.IsNullOrEmpty(condString)) { sb.AppendLine(string.Format("WHERE {0}", condString)); cmd.Parameters.AddRange(cond.GetSqlParameters().ToArray()); } } cmd.CommandText = sb.ToString(); }