Multiple Insert statements in one connection

前端 未结 4 1718
南旧
南旧 2021-01-24 00:05

I need some tips on how to do this better, I am inserting multiple queries with using one connection.

I understand this is not good programming, especi

4条回答
  •  孤独总比滥情好
    2021-01-24 00:44

    You should parameterized your query - ALWAYS, but for now you can concatenate those queries with ; and then execute them once like:

    string allQueries = string.join(';', query2, query3, query4, query5);
    command.CommandText = allQueries; 
    int c = command.ExecuteNonQuery();
    

    Currently you are just executing one query. Semicolon ; marks end of statement in SQL, so combining these statements with ; will make them separate statements but they will be executed under one execution.

    kcray - This is what worked for me.

     string[] arr = { query2, query3 };
     string allQueries = string.Join(";", arr);
     command.CommandText = allQueries;
     int c = command.ExecuteNonQuery();
    

提交回复
热议问题