A
A
Anton_repr2019-11-04 11:43:49
MySQL
Anton_repr, 2019-11-04 11:43:49

How to add multiple database items using one query?

The database has a table in which you need to write 3 elements. Here is my code:

var recipe = Tbx_recipeName.Text;
var description = Tbx_recipeDescription.Text;
var instruction = Tbx_instructions.Text;
                string sqlcom = $"INSERT INTO recipel VALUES (0, '{recipe}')";
                string sqlcomm = $"INSERT INTO recipel VALUES (1, '{description}')";
                string sqlcommand = $"INSERT INTO recipel VALUES (2, '{instruction}')";

                using (MySqlCommand command = new MySqlCommand(sqlcom, connectToDB.connection))
                    command.ExecuteNonQuery();
                using (MySqlCommand command = new MySqlCommand(sqlcomm, connectToDB.connection))
                    command.ExecuteNonQuery();
                using (MySqlCommand command = new MySqlCommand(sqlcommand, connectToDB.connection))
                    connectToDB.CloseConnect();

I understand that my code is terrible and so on. Could you please shorten it?
I tried to shorten it like this:
string sqlcom = $"INSERT INTO recipel VALUES (0, '{recipe}'), (1, '{description}'), (2, '{instruction}')";

Did I do everything right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2019-11-04
@Anton_repr

INSERT INTO tbl_name (a,b,c) 
VALUES 
  (1,2,3),
  (4,5,6),
  (7,8,9);

https://dev.mysql.com/doc/refman/5.5/en/insert.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question