Example #1
0
        /// <summary>
        /// Gets a SQL WHERE clause from the parameters and comparison type
        /// </summary>
        internal static string WhereClauseFromParameters(SqlParameter[] sqlParams, GetRecordOperator comparison)
        {
            if (sqlParams.FirstOrDefault(p => string.IsNullOrWhiteSpace(p.SourceColumn)) != null) throw new DataException("All source columns must be defined");
            foreach(SqlParameter p in sqlParams)
            {
                if (p.ParameterName == "") p.ParameterName = "@" + p.SourceColumn.Replace(" ", "_");
                if (p.ParameterName.Substring(0, 1) != "@") p.ParameterName = "@" + p.ParameterName;
            }

            string sql = "where ";
            string clause = (comparison == GetRecordOperator.And) ? " and ": " or ";

            bool first = true;
            foreach(SqlParameter p in sqlParams)
            {
                if (!first) sql += clause;
                sql += "[" + p.SourceColumn + "] ";
                if(p.Value == null)
                {
                    sql += "is null";
                }else
                {
                    sql += "= " + p.ParameterName;
                }
                first = false;
            }

            return sql;
        }