Example #1
0
        private static DbCommand CreateInsertCommand(string tName, Dictionary <string, object> keyValues)
        {
            var cols = keyValues.Keys.Aggregate("", (a, b) => a + RedirectSqlTableColName(b) + ",").TrimEnd(',');
            var vals = keyValues.Keys.Aggregate("", (a, b) => a + "@" + RedirectSqlTableColName(b) + ",").TrimEnd(',');
            var cmd  = SqlHelper.CreateCommand(string.Format("INSERT INTO {0}({1}) VALUES({2})", tName, cols, vals), Repo.Connection);

            foreach (var key in keyValues.Keys)
            {
                var val = keyValues[key];

                if (val != null && val is string)
                {
                    FixTextCol(tName, key, val as string);
                }

                var para = SqlHelper.CreateDbParameter("@" + RedirectSqlTableColName(key), val);
                cmd.Parameters.Add(para);
                if (val == null)
                {
                    para.Value = DBNull.Value;
                }
                if (val is DateTime && (((DateTime)val).Year <= 1753 || ((DateTime)val).Year >= 9999))
                {
                    para.Value = DBNull.Value;
                }
            }

            return(cmd);
        }