public async Task <World[]> LoadMultipleUpdatesRows(int count)
        {
            using (var db = _dbProviderFactory.CreateConnection())
            {
                db.ConnectionString = _connectionString;
                await db.OpenAsync();

                using (var updateCmd = db.CreateCommand())
                    using (var queryCmd = CreateReadCommand(db))
                    {
                        var results = new World[count];
                        for (int i = 0; i < count; i++)
                        {
                            results[i] = await ReadSingleRow(db, queryCmd);

                            queryCmd.Parameters["@Id"].Value = _random.Next(1, 10001);
                        }

                        updateCmd.CommandText = BatchUpdateString.Query(count);

                        for (int i = 0; i < count; i++)
                        {
                            var id = updateCmd.CreateParameter();
                            id.ParameterName = $"@Id_{i}";
                            id.DbType        = DbType.Int32;
                            updateCmd.Parameters.Add(id);

                            var random = updateCmd.CreateParameter();
                            random.ParameterName = $"@Random_{i}";
                            random.DbType        = DbType.Int32;
                            updateCmd.Parameters.Add(random);

                            var randomNumber = _random.Next(1, 10001);
                            id.Value                = results[i].Id;
                            random.Value            = randomNumber;
                            results[i].RandomNumber = randomNumber;
                        }

                        await updateCmd.ExecuteNonQueryAsync();

                        return(results);
                    }
            }
        }
Exemple #2
0
        private static DbCommand CreatCommand(int count)
        {
            DbCommand cmd = new Npgsql.NpgsqlCommand();

            cmd.CommandText = BatchUpdateString.Query(count);
            for (int i = 0; i < count; i++)
            {
                var id = cmd.CreateParameter();
                id.ParameterName = IDParamereNames[i];
                id.DbType        = DbType.Int32;
                cmd.Parameters.Add(id);

                var random = cmd.CreateParameter();
                random.ParameterName = RandomParamereNames[i];
                random.DbType        = DbType.Int32;
                cmd.Parameters.Add(random);
            }
            return(cmd);
        }
Exemple #3
0
        public async Task <World[]> LoadMultipleUpdatesRows(int count)
        {
            var results = new World[count];

            using (var db = new NpgsqlConnection(_connectionString))
            {
                await db.OpenAsync();

                var(queryCmd, queryParameter) = CreateReadCommand(db);
                using (queryCmd)
                {
                    for (int i = 0; i < results.Length; i++)
                    {
                        results[i] = await ReadSingleRow(queryCmd);

                        queryParameter.TypedValue = _random.Next(1, 10001);
                    }
                }

                using (var updateCmd = new NpgsqlCommand(BatchUpdateString.Query(count), db))
                {
                    var ids     = BatchUpdateString.Ids;
                    var randoms = BatchUpdateString.Randoms;

                    for (int i = 0; i < results.Length; i++)
                    {
                        var randomNumber = _random.Next(1, 10001);

                        updateCmd.Parameters.Add(new NpgsqlParameter <int>(parameterName: ids[i], value: results[i].Id));
                        updateCmd.Parameters.Add(new NpgsqlParameter <int>(parameterName: randoms[i], value: randomNumber));

                        results[i].RandomNumber = randomNumber;
                    }

                    await updateCmd.ExecuteNonQueryAsync();
                }
            }

            return(results);
        }