Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save data from rows to database

Refer the code below:

void loadInstallMentPattern(System.Collections.ArrayList pattern)
    {
        dataGridView1.Rows.Clear();

        for (int i = 0; i < pattern.Count; i++)
        {
            int c = dataGridView1.Rows.Add();
            dataGridView1.Rows[c].Cells["gvcSNo"].Value = (i + 1).ToString();
            dataGridView1.Rows[c].Cells["gvcDueDate"].Value = ((InstallmentPatternStruct)pattern[i]).DueDate;
            dataGridView1.Rows[c].Cells["gvcAmount"].Value = ((InstallmentPatternStruct)pattern[i]).PrincipalAmt;
            dataGridView1.Rows[c].Cells["gvcInterestAmt"].Value = ((InstallmentPatternStruct)pattern[i]).InterestAmt;

            dataGridView1.Rows[c].Cells["gvcDebitAmt"].Value = ((InstallmentPatternStruct)pattern[i]).DebitPrincipalAmt;
            dataGridView1.Rows[c].Cells["gvcEMI"].Value = ((InstallmentPatternStruct)pattern[i]).EMI;
        }
    }

I have pragmatically added a few rows to DataGridView which are required to be further send to database for persistence.

Currently I am sending the data by reading each row from grid and then sending it to db. This means if I've 500 rows in DataGridView, then I'll have to fire 500 Insert queries.

I was wondering is there any other way to send data to db(in bulk) in case where DataGRidView is not data bound.

I hope I am able to explain my problem clearly. Any help would be greatly appreciated.

like image 801
Amit Mittal Avatar asked Dec 15 '25 14:12

Amit Mittal


1 Answers

There's probably a way to do it closer to how .NET objects let you do it, but there's always, as a fallback,

INSERT INTO Table ( ColumnA, ColumnB ) 
VALUES ( ValueA1, ValueB1 ), ( ValueB2, ValueB2 ), ... ( ValueAn, ValueBn)
like image 98
Andrew Stollak Avatar answered Dec 17 '25 05:12

Andrew Stollak