Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need to insert data through a form using a datagridview connected to a SQL Server 2008 R2 database. is it ok, if I write a single insert stored procedure query in a while loop in the form.cs

share|improve this question
2  
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask –  gnat Sep 11 '13 at 10:30
1  
"is it ok, if i write a single insert stored procedure query in a while loop in the c# form.cs" How have you done this and what were the results? That may help people decide whether or not it is ok, or whether you need something better. –  Tom Au Sep 11 '13 at 16:34
add comment

1 Answer

In general you want to minimize the number of calls you make from the client to the database. You don't want the client to call the database with every row when you know that you want to add all rows. One way to achieve what you want is to send a group of rows all at one time to the stored procedure. "

This article describes how to use table-valued parameters when calling Stored Procedures. Table-valued parameters were introduced in SQL Server 2008. With this parameter type, a table with several rows can be passed to a Stored Procedure": Send multiple rows to stored procedure.

You need to be careful with validation prior to inserts and perform such an insert in a single transaction otherwise, coding for rows that flailed to insert will require more work.

If your scenario involves file loading into SQL Server, you may want to consider an alternative efficient method such as Bulk Insert.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.