I don't know of a way in MySQL to do this in one go, but there are other options:
1. Create a trigger for the table
Create a trigger with the trigger time = BEFORE and the trigger event = INSERT.
Inside the trigger you check if the number of rows and if it the number is >30 you signal an error.
See
MySQL :: MySQL 5.7 Reference Manual :: 14.1.20 CREATE TRIGGER Syntax[
^]
See also
sql - MySQL Trigger to prevent INSERT under certain conditions - Stack Overflow[
^]
(The correct solution depends on the version of MySQL)
2. Create a stored procedure
This is probably the best option as you only do one external transaction and you can notify the user.
See
MySQL :: MySQL 5.7 Reference Manual :: 14.1.16 CREATE PROCEDURE and CREATE FUNCTION Syntax[
^]
and
Getting Started with MySQL Stored Procedures[
^]
Then you call the stored procedure from C# code. See
Working C# code for MySql Stored Procedures IN, OUT, and INOUT parameters[
^]
3. Do it with multiple calls in C#
In this approach you you first send a request to get to know how many rows there are
string cmd = "SELECT count(field_name) FROM Batch;";
Then use MySqlCommand.ExecuteScalar()
then check if the number of rows are <30 and then send the insertion code.
string cmd = "INSERT INTO `Batch` (field_name) VALUES ('What Ever');";
Here you should use a parameterized query like shown in the CodeProject article above.
Your setup is a bit weird, however.
Doing it this way you would need to either create one table per batch or delete all rows in your table before adding the next batch.
Have you considered to add a column that contains the date when you added the row, and then check if there are more than 30 rows within the same month.
This way you will also have a history of all batches in one table.