示例#1
0
        public object InsertV2 <T>(T t) where T : IDicSerialize
        {
            IDictionary <string, MyField> dic = t.ToDic();
            string        tbname = t.TableName;
            StringBuilder sb     = new StringBuilder(" insert into  ");

            sb.AppendFormat(tbname);
            sb.AppendFormat("(");
            int dicCount = dic.Count;
            int count    = 0;

            foreach (string key in dic.Keys)
            {
                count++;
                if (!_executor.IsInsertColumn(key, dic))
                {
                    continue;
                }
                if (count == dicCount)
                {
                    sb.AppendFormat("[{0}]) values (", key);
                }
                else
                {
                    sb.AppendFormat("[{0}],", key);
                }
            }
            count = 0;
            foreach (string key in dic.Keys)
            {
                count++;
                if (!_executor.IsInsertColumn(key, dic))
                {
                    continue;
                }
                if (count == dicCount)
                {
                    sb.AppendFormat("@{0})", key);
                }
                else
                {
                    sb.AppendFormat("@{0},", key);
                }
            }
            object last = 0;

            _executor.ExecuteNonQuery(sb.ToString(), CommandType.Text, CreateParameter(dic));
            last = _executor.ExecuteScalar(string.Format("SELECT  max({1}) from [{0}]", tbname, GetIdentityColumnName <T>(t)));
            return(last);
        }
示例#2
0
        public object Insert <T>(T t)
        {
            StringBuilder       sb   = null;
            List <PropertyInfo> list = DataMapper.GetProperties <T>();

            string tbName = typeof(T).Name;

            #region  获取表中的所有列
            List <string> columns = new List <string>();
            columns = GetAccessTbColumns(conn, tbName);
            list    = list.Where(x => columns.Contains(x.Name, _compare)).ToList();
            #endregion

            #region 判断需要过滤的字段
            //不需要插入数据库的列
            List <string> filteredColumns = ReflectionHelper.GetNotInsertedColumns <T>();
            //new List<string>();
            //filteredColumns.Add(ReflectionHelper.GetTypeField_IdentityKeys<T>());
            //返回过滤后的列列表
            if (filteredColumns.Count > 0)
            {
                list = list.Where(x => !filteredColumns.Contains(x.Name, _compare)).ToList();
            }
            #endregion

            int size = list.Count;
            sb = new StringBuilder(string.Format("insert into [{0}] (", tbName));
            for (int i = 0; i < size; i++)
            {
                if (filteredColumns.Contains(list[i].Name, _compare))
                {
                    continue;
                }
                if (i != size - 1)
                {
                    sb.AppendFormat("[{0}],", list[i].Name);
                }
                else
                {
                    sb.AppendFormat("[{0}]) values(", list[i].Name);
                }
            }
            object       last = "0";
            PropertyInfo pi   = null;
            for (int i = 0; i < size; i++)
            {
                pi = list[i];
                if (filteredColumns.Contains(list[i].Name, _compare))
                {
                    continue;
                }
                if (i != size - 1)
                {
                    sb.AppendFormat("@{0},", pi.Name);
                }
                else
                {
                    sb.AppendFormat("@{0})", pi.Name);
                }
            }
            _executor.ExecuteNonQuery(sb.ToString(), CommandType.Text, CreateParameter(list, filteredColumns, t, _compare));
            last = _executor.ExecuteScalar("SELECT  max(idx) from [" + tbName + "]");
            return(last);
        }