示例#1
0
        /// <summary>
        /// Insert一条数据
        /// </summary>
        /// <param name="bean"></param>
        /// <returns></returns>
        bool Insert(BaseEntity bean)
        {
            if (bean == null || bean.Count == 0)
            {
                throw new Exception("bean 不能为 NULL");
            }

            List <string> _Values = new List <string>();

            _Columns.Clear();
            Params.Clear();
            foreach (var item in bean)
            {
                _Columns.Add(DB.GetName(item.Key));
                _Values.Add(DB._ParameterPrefix + item.Key);
                Params.Add(DB.GetParam(item.Key, item.Value.ToString()));
            }
            var sql = "INSERT INTO " + GetTableName(bean) + " (" + string.Join(",", _Columns) + ") VALUES (" + string.Join(",", _Values) + "); select ROW_COUNT(),LAST_INSERT_ID();";
            var id  = -1L;

            try
            {
                if (Db.Insert(sql, Params, ref id))
                {
                    if (id > 0)
                    {
                        bean["ID"] = id;
                    }
                    return(true);
                }
                return(false);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (ShowSQL)
                {
                    ShowSQLString(sql, Params);
                }
            }
        }