Ejemplo n.º 1
0
        /// <summary>
        /// 根据条件获取实体数据
        /// </summary>
        /// <typeparam name="T">要获取的实体</typeparam>
        /// <returns></returns>
        public static T GetModels <T>(string WHERE) where T : new()
        {
            string StrSQL  = string.Empty;
            string FAILEDS = string.Empty;

            try
            {
                T    t    = new T();
                Type type = t.GetType();
                StrSQL = SQL_SELECT.Replace("{TABLE_NAME}", type.Name);
                foreach (PropertyInfo Info in type.GetProperties())
                {
                    FAILEDS += Info.Name + ",";
                }

                if (FAILEDS.Length > 0)
                {
                    FAILEDS = FAILEDS.Remove(FAILEDS.Length - 1);
                }
                else
                {
                    FAILEDS = "*";
                }

                StrSQL = StrSQL.Replace("{FAILEDS}", FAILEDS);
                //判断条件是否为空
                if (!string.IsNullOrWhiteSpace(WHERE))
                {
                    StrSQL = StrSQL.Replace("{VALUES}", WHERE);
                }
                else
                {
                    StrSQL = StrSQL.Replace("{VALUES}", " ");
                }

                return(GetModel <T>(ExecuteReader(StrSQL)));
            }
            catch (Exception EX)
            {
                throw new Exception(EX.Message, EX);
            }
        }
Ejemplo n.º 2
0
        private static void ExecuteSqlBulkCopy(SqlTransaction transaction, SqlConnection sourceConnection, SqlConnection destinationConnection, SqlBulkCopyOptions copyOptions, string tableName, IEnumerable <string> columnNames)
        {
            using var sourceCommand   = sourceConnection.CreateCommand();
            sourceCommand.CommandText = SQL_SELECT.FormatInvariant(tableName);
            var sourceTableReader = sourceCommand.ExecuteReader();

            using var sqlBulkCopy = new SqlBulkCopy(destinationConnection, copyOptions, transaction)
                  {
                      BatchSize            = BULKCOPY_BATCHSIZE,
                      BulkCopyTimeout      = 0,
                      DestinationTableName = tableName,
                      EnableStreaming      = true,
                  };
            foreach (var columnName in columnNames)
            {
                _ = sqlBulkCopy.ColumnMappings.Add(columnName, columnName);
            }

            sqlBulkCopy.WriteToServer(sourceTableReader);
            sourceTableReader.Close();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据条件实体获取实体数据
        /// </summary>
        /// <typeparam name="T">要获取的实体</typeparam>
        /// <returns></returns>
        public static T GetModels <T>(T Model) where T : new()
        {
            string StrSQL  = string.Empty;
            string FAILEDS = string.Empty;
            string WHERE   = string.Empty;

            try
            {
                //T t = new T();
                Type type = Model.GetType();
                StrSQL = SQL_SELECT.Replace("{TABLE_NAME}", type.Name);
                foreach (PropertyInfo Info in type.GetProperties())
                {
                    FAILEDS += Info.Name + ",";

                    //获取条件
                    if (null != Info.GetValue(Model, null))
                    {
                        WHERE += " AND " + Info.Name + " ='" + Info.GetValue(Model, null) + "' ";
                    }
                }

                if (FAILEDS.Length > 0)
                {
                    FAILEDS = FAILEDS.Remove(FAILEDS.Length - 1);
                }
                else
                {
                    FAILEDS = "*";
                }

                StrSQL = StrSQL.Replace("{FAILEDS}", FAILEDS);
                StrSQL = StrSQL.Replace("{VALUES}", WHERE);
                return(GetModels <T>(ExecuteReader(StrSQL)));
            }
            catch (Exception EX)
            {
                throw new Exception(EX.Message, EX);
            }
        }