Example #1
0
        public string ToString(IBaseDataSpecial cfg)
        {
            StringBuilder sql = new StringBuilder();

            IUpatedataInfo _row = this.FilterRow;

            foreach (string f in _row.GetUpateFields())
            {
                object v = _row.GetValueByName(f);
                if (v == null || v == DBNull.Value)
                {
                    switch (cfg.FieldMask)
                    {
                    case EM_Safe_Field_MASK.MySQLMode:
                        sql.AppendFormat(" and isNull(`{0}`)=1", f); continue;

                    case EM_Safe_Field_MASK.MsSQLMode:
                        sql.AppendFormat(" and [{0}] is null", f); continue;

                    default:
                        throw new Exception("未知的编码方式:" + cfg.FieldMask.ToString());
                    }
                }

                if (v is int)
                {
                    sql.AppendFormat(" and {0}={1}", cfg.FieldEncode(f), v);
                }
                else
                {
                    sql.AppendFormat(" and {0}='{1}'", cfg.FieldEncode(f), cfg.SqlEncode(v.ToString()));
                }
            }

            if (_andFilter != null && _andFilter.Count != 0)
            {
                sql.AppendFormat(" and {0}", DataFilter.FiltersToString(_andFilter, "and", cfg));
            }

            if (_orFilter != null && _orFilter.Count != 0)
            {
                sql.AppendFormat("\n or {0}", DataFilter.FiltersToString(_orFilter, "or", cfg));
            }
            if (sql.Length > 4)
            {
                string h = sql.ToString().Substring(0, 4);

                if (h == " and" || h == "\n or")
                {
                    sql.Remove(0, 4);
                }
            }
            return(sql.ToString());
        }
Example #2
0
        protected override bool SaveData(IUpatedataInfo DataRow)
        {
            string table = DataRow.TableName;

            if (string.IsNullOrEmpty(table))
            {
                return(false);
            }
            string idStr = DataRow.IdentifyField;

            if (string.IsNullOrEmpty(idStr))
            {
                return(false);
            }
            List <string> fields = DataRow.GetUpateFields();

            if (fields.Count == 0)
            {
                return(false);
            }

            using (var cmd = Command())
            {
                bool   isNew;
                object t = DataRow.GetValueByName(idStr);
                isNew = t == null || t == DBNull.Value || (int)t <= 0;

                SqlParameter ptr;
                string       sql1 = string.Empty, sql2 = string.Empty;

                foreach (string f in fields)
                {
                    if (f == idStr)
                    {
                        continue;
                    }
                    var val = DataRow.GetValueByName(f);
                    if (val == null)
                    {
                        val = DBNull.Value;
                    }
                    ptr = new SqlParameter("@" + f, val);
                    cmd.Parameters.Add(ptr);
                    if (isNew)
                    {
                        sql1 += ",[" + f + "]";
                        sql2 += ",@" + f;
                    }
                    else
                    {
                        sql1 += ",[" + f + "]=@" + f;
                    }
                }
                if (!string.IsNullOrEmpty(sql1))
                {
                    sql1 = sql1.Substring(1);
                }
                string sql;
                if (isNew)
                {
                    sql = "insert into [" + table + "](" + sql1 + ") values(" + sql2.Substring(1) + ")";
                }
                else
                {
                    sql = "update [" + table + "] set " + sql1 + " where [" + idStr + "]=@" + idStr;
                    ptr = new SqlParameter("@" + idStr, DataRow.GetValueByName(idStr));
                    cmd.Parameters.Add(ptr);
                }

                cmd.CommandText = sql;
                TimerStart();
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    Shotgun.Library.ErrLogRecorder.SqlError(ex, sql, cmd.Parameters);
                    cmd.Dispose();
                    throw;
                }
                finally
                {
                    TimerEnd(sql);
                }

                if (isNew)
                {
                    cmd.CommandText = "select @@identity";
                    t = cmd.ExecuteScalar();
                }
                else
                {
                    t = null;
                }
                cmd.Dispose();

                DataRow.SetUpdated(t);
            }
            return(true);
        }