Exemple #1
0
        public async Task <IEnumerable <dynamic> > ConvertAsync(MsSqlDbAttribute input, CancellationToken cancellationToken)
        {
            var context = _msSqlDbExtensionConfigProvider.CreateContext(input);

            var sqlSpec = new MsSqlSpec()
            {
                Parameters = input.SqlQueryParameters,
                Query      = input.SqlQuery
            };

            var list = new List <object>();

#if NETCOREAPP3_1
            var objects = context.MsSqlDbService.GetAsync(sqlSpec, cancellationToken);

            await foreach (var o in objects.WithCancellation(cancellationToken))
            {
                list.Add(o);
            }
#else
            var objects = await context.MsSqlDbService.GetAsync(sqlSpec, cancellationToken);

            list.AddRange(objects);
#endif
            return(list.AsEnumerable());
        }
        public async Task <T> GetOne <T>(MsSqlSpec msSqlSpec, CancellationToken cancellationToken = default)
            where T : class, new()
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(msSqlSpec.Query, connection);
                foreach (var sqlParameter in msSqlSpec.Parameters)
                {
                    command.Parameters.Add(sqlParameter);
                }

                await connection.OpenAsync(cancellationToken);

                var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection, cancellationToken);

                var isRead = await reader.ReadAsync(cancellationToken);

                if (isRead)
                {
                    return(reader.ConvertToObject <T>());
                }

                return(default(T));
            }
        }
Exemple #3
0
        public Task <T> ConvertAsync(MsSqlDbAttribute input, CancellationToken cancellationToken)
        {
            var context = _msSqlDbExtensionConfigProvider.CreateContext(input);

            var sqlSpec = new MsSqlSpec()
            {
                Parameters = input.SqlQueryParameters,
                Query      = input.SqlQuery
            };

            return(context.MsSqlDbService.GetOne <T>(sqlSpec, cancellationToken));
        }
Exemple #4
0
        public IEnumerable <T> Get <T>(MsSqlSpec msSqlSpec) where T : class, new()
        {
            using var connection = new SqlConnection(_connectionString);
            var command = new SqlCommand(msSqlSpec.Query, connection);

            foreach (var sqlParameter in msSqlSpec.Parameters)
            {
                command.Parameters.Add(sqlParameter);
            }
            connection.Open();
            var reader = command.ExecuteReader(CommandBehavior.CloseConnection);

            while (reader.Read())
            {
                yield return(reader.ConvertToObject <T>());
            }
        }
Exemple #5
0
        public async IAsyncEnumerable <object> GetAsync(MsSqlSpec msSqlSpec, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            await using var connection = new SqlConnection(_connectionString);
            var command = new SqlCommand(msSqlSpec.Query, connection);

            foreach (var sqlParameter in msSqlSpec.Parameters)
            {
                command.Parameters.Add(sqlParameter);
            }
            await connection.OpenAsync(cancellationToken);

            var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection, cancellationToken);

            while (await reader.ReadAsync(cancellationToken))
            {
                yield return(reader.ConvertToObject());
            }
        }
        public async Task <IEnumerable <object> > GetAsync(MsSqlSpec msSqlSpec, CancellationToken cancellationToken = default)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(msSqlSpec.Query, connection);
                foreach (var sqlParameter in msSqlSpec.Parameters)
                {
                    command.Parameters.Add(sqlParameter);
                }

                await connection.OpenAsync(cancellationToken);

                var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection, cancellationToken);

                var list = new List <object>();
                while (await reader.ReadAsync(cancellationToken))
                {
                    list.Add(reader.ConvertToObject());
                }

                return(list.AsEnumerable());
            }
        }