Click here to Skip to main content
15,915,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there everyone, I'm quite new in c# and SQL and I just wanna know how to actually do a loop for inserting the same data into a database. I need to insert the same data into my database for a total amount of 5000 times. And for extra information, I'm using SQL compact edition (.sdf) type of database. Thanks in advance.(^_^)
Posted
Comments
[no name] 6-Mar-12 20:14pm    
Why on earth would you want to do such a thing?
Orcun Iyigun 6-Mar-12 20:21pm    
I totally agree, seems like he wants to learn it the hard way :)

1 solution

With a little bit of Google search you could have found an answer to your question. But anyways,

C#
int loopCount = 5000;
for (int i = 0; i < loopCount; i++)
{
  //Your DB insert code here
}

-OR-
C#
int counter = 0;
while (counter != loopCount)
{
  //Your DB insert code here
  counter++;
}


For more info on the loops check these links:
Loops Tutorial 1[^]
Loops Tutorial 2[^]

Good luck,
OI
 
Share this answer
 
Comments
nizam15 6-Mar-12 20:22pm    
Thanks a bunch~
nizam15 7-Mar-12 0:50am    
just a question..is there a way to fasten the data insertion. Because when inserting 20k of data, it will get really slow~
Orcun Iyigun 7-Mar-12 2:38am    
Look into the bulk insert tools such as: Bulk Insert(T-SQL)[^] or SQLBulkCopy Class(.Net)[^].

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