Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
is this efficient way of writing the stored procedure in a loop??

for (int i = 0; i < purchaseListView.Items.Count; i++)
                     {
Connection con = new Connection();
                     SqlCommand cmd = new SqlCommand();
                     SqlCommand cmdFifo = new SqlCommand();
                     con.OpenConnection();
                     cmd.Connection = con.DataBaseConnection;
                     cmd.CommandType = CommandType.StoredProcedure;
                     cmd.CommandText = "insertDetail";
                     cmdFifo.Connection = con.DataBaseConnection;
                     cmdFifo.CommandType = CommandType.StoredProcedure;
                     cmdFifo.CommandText = "insertInToMain";

please let me know the right way of looping the stored procedure
best regards
bunzitop
Posted
Updated 15-Mar-13 5:38am
v2

1 solution

No, because you open the connection again and again and again. Outside your loop, open the connection.
 
Share this answer
 
Comments
bunzitop 15-Mar-13 11:47am    
suppose i kept the connection outside loop, then will it be standard form of codding and efficient way of coding ??? will my code perform high??
ZurdoDev 15-Mar-13 11:51am    
For the most part. You can also move the creation of the Command object out and then just clear and readd parameters inside the loop. You might also want to look into using the keyword "using".
Sergey Alexandrovich Kryukov 15-Mar-13 15:04pm    
Good point, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900