示例#1
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);
        }
示例#2
0
        public object Insert <T>(T dbModel)
        {
            StringBuilder       sb   = null;
            List <PropertyInfo> list = DataMapper.GetProperties <T>();


            List <string> filteredColumns = null; filteredColumns = new List <string>();
            string        tbName          = typeof(T).Name;

            #region  获取表中的所有列
            List <string> columns = new List <string>();
            if (_dbProvider is OleDbProvider)
            {
                columns = GetAccessTbColumns((OleDbConnection)CreateConnection(), tbName);
            }

            list = list.Where(x => columns.Contains(x.Name, _compare)).ToList();
            #endregion


            #region 判断需要过滤的字段

            if (_dbProvider is SqlProvider)
            {
                string schema      = ExecuteScalar(string.Format("select top 1 [table_schema] from information_schema.columns where TABLE_NAME='{0}'", tbName)).ToString();
                string fulelTbName = string.Format("{0}.{1}", schema, tbName);
                sb = new StringBuilder(string.Format("insert into {0} (", fulelTbName));
                string sql = string.Format("select [name] from sys.columns WHERE object_id=OBJECT_ID('{0}') AND is_identity=1", fulelTbName);
                using (IDataReader dr = ExecuteReader(sql))
                {
                    while (dr.Read())
                    {
                        if (filteredColumns == null)
                        {
                            filteredColumns = new List <string>();
                        }
                        filteredColumns.Add(dr[0].ToString());
                    }
                }
            }
            else if (_dbProvider is OleDbProvider)
            {
                sb = new StringBuilder(string.Format("insert into {0} (", tbName));
                filteredColumns.Add(ReflectionHelper.GetTypeField_IdentityKeys <T>());
            }
            else if (_dbProvider is MySqlProvider)
            {
                sb = new StringBuilder(string.Format("insert into {0} (", string.Format("{0}", tbName)));
            }
            #endregion

            int size = list.Count;
            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";
            if (_dbProvider is MySqlProvider)
            {
                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);
                    }
                }
                last = ExecuteScalar("SELECT  LAST_INSERT_ID()");
            }
            else if (_dbProvider is SqlProvider)
            {
                PropertyInfo pi = null;
                for (int i = 0; i < size; i++)
                {
                    pi = list[i];
                    if (filteredColumns.Contains(list[i].Name))
                    {
                        continue;
                    }
                    if (i != size - 1)
                    {
                        sb.AppendFormat("@{0},", pi.Name);
                    }
                    else
                    {
                        sb.AppendFormat("@{0})", pi.Name);
                    }
                }
                last = ExecuteScalar("SELECT  @@identity");
            }
            else if (_dbProvider is OleDbProvider)
            {
                //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);
                //    }
                //}
                //ExecuteNonQuery(sb.ToString(), CommandType.Text, CreateParameter(list, dbModel));
                //last = ExecuteScalar("SELECT  max(idx) from " + tbName);
                operation = new OledbEntityOperation(this);
                last      = operation.Insert <T>(dbModel);
            }

            //   HttpContext.Current.Response.Write(sb.ToString());

            return(last);
        }