简体   繁体   中英

back-end to database approach

I'm using asp.net core as back-end and mySQL as database. I got to a point where I'm getting arrays from the front-end to insert into my database. My question is: Should I iterate on the array and send multiple requests to database, or is it more "cheap" to send the data as strings and decompose the data in the database?

example:

let array = [{id: 1, name: "a"},{id:2, name:"b"}];

option 1:

//for each data in array -> send insert query

option 2:

//create two string
let ids = "1,2";
let names = "a,b";

//send 1 request to database, 
//use stored procedure with methods like `SUBSTRING_INDEX` and `INSTR` to decompose the data from strings.

An healthy explanation would be helpful.

Dynamic SQL can be helpful here. Create a dynamic query in code to insert all the row in one go into database. You can do that as given below

INSERT INTO tab(id,name)
VALUES (1,'a'),
       (2,'b');

请考虑使用实体框架Microsoft文档

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM