public void ExecuteInvalidSqlBatch() { using (var connection = new MySqlConnection(AppConfig.ConnectionString)) { connection.Open(); using (var batch = new MySqlBatch(connection) { BatchCommands = { new MySqlBatchCommand("SELECT 1;"), new MySqlBatchCommand("SELECT 2 /* incomplete"), new MySqlBatchCommand("SELECT 3;"), }, }) using (var reader = batch.ExecuteReader()) { Assert.True(reader.Read()); Assert.Equal(1, reader.GetInt32(0)); Assert.False(reader.Read()); try { reader.NextResult(); Assert.True(false, "Shouldn't get here"); } catch (MySqlException ex) { Assert.Equal(MySqlErrorCode.ParseError, (MySqlErrorCode)ex.Number); } } } }
public void ExecuteBatch(string suffix) { using var connection = new MySqlConnection(AppConfig.ConnectionString); connection.Open(); using var batch = new MySqlBatch(connection) { BatchCommands = { new MySqlBatchCommand("SELECT 1" + suffix), new MySqlBatchCommand("SELECT 2" + suffix), new MySqlBatchCommand("SELECT 3" + suffix), }, }; using var reader = batch.ExecuteReader(); var total = 0; Assert.True(reader.Read()); total += reader.GetInt32(0); Assert.False(reader.Read()); Assert.True(reader.NextResult()); Assert.True(reader.Read()); total += reader.GetInt32(0); Assert.False(reader.Read()); Assert.True(reader.NextResult()); Assert.True(reader.Read()); total += reader.GetInt32(0); Assert.False(reader.Read()); Assert.False(reader.NextResult()); Assert.Equal(6, total); }
public void SingleRow() { using (var connection = new MySqlConnection(AppConfig.ConnectionString)) { connection.Open(); using (var command = new MySqlCommand(@"drop table if exists batch_single_row; create table batch_single_row(id integer not null primary key); insert into batch_single_row(id) values(1),(2),(3);", connection)) { command.ExecuteNonQuery(); } using (var batch = new MySqlBatch(connection) { BatchCommands = { new MySqlBatchCommand("SELECT id FROM batch_single_row ORDER BY id"), new MySqlBatchCommand("SELECT id FROM batch_single_row ORDER BY id") { CommandBehavior = CommandBehavior.SingleRow }, }, }) using (var reader = batch.ExecuteReader()) { Assert.True(reader.Read()); Assert.Equal(1, reader.GetInt32(0)); Assert.True(reader.Read()); Assert.Equal(2, reader.GetInt32(0)); Assert.True(reader.Read()); Assert.Equal(3, reader.GetInt32(0)); Assert.False(reader.Read()); Assert.True(reader.NextResult()); Assert.True(reader.Read()); Assert.Equal(1, reader.GetInt32(0)); Assert.False(reader.Read()); Assert.False(reader.NextResult()); } } }