I have a form with a blank datagridview. What's the most effective way to get the mulptiple lines of data from the datagridview saved into the database? I've searched for tutorials and have been recommended SqlBulkCopy and a few other solutions. The aim is to use it to create invoices, so the names of the tables would need to be unique. I'm using SqlCe and C# 2010.
Asked
Active
Viewed 298 times
0
-
I haven't tried anything as of yet, for some reason bulkcopy doesn't exist in SqlCe? – Tom May 28 '14 at 16:21
-
Hm. I've never used BulkCopy.. The way I do it is grab each cell value individually, paramterize it and write a SQL statement using thos evalues, and iterate over each row in the DGV. – Mark C. May 28 '14 at 16:23
-
I read somewhere that way can be slow when copying lots of data. I'd say on average I'd need to insert 100 rows max. But into a unique table – Tom May 28 '14 at 16:24
-
would [this](http://stackoverflow.com/questions/1606487/sqlbulkcopy-using-sql-ce) work ? – Tom May 28 '14 at 16:28
-
I have actually never tried that. I would say go for it. If you encounter difficulties, come back and ask a specific question. If that approach doesn't work for you, see [here](http://stackoverflow.com/questions/10997716/what-is-the-best-way-to-traverse-datagridview) for a related thread. – Mark C. May 28 '14 at 16:33
-
Could you post how you would do it as an answer? So I can see what code etc. I could use – Tom May 28 '14 at 16:35
1 Answers
0
I refer to each cell specifically (using index), paramaterize that value and use it in an INSERT
statement.
See here:
SqlCommand cmd = new SqlCommand();
//Basic SQL INSERT statement
cmd.CommandText += " INSERT INTO ELECT_SALARY_DFRL (ELECT_XREF_SEQ_ID, EMPL_SALARY_DFRL_AMT, EMPL_SALARY_DFRL_FREQ_CD, ";
cmd.CommandText += " FUND_SEQ_ID, ELECT_SALARY_DFRL_FUND_PCT_RT, ELECT_SALARY_DFRL_ELECT_DT, BEG_DT, END_DT)";
cmd.CommandText += " VALUES (@SeqID, @DefAmt, @Freq, @FundID, @FundPct, @Today, @begDt, @EndDT); ";
//Parameters for sql insert, from DGV.
cmd.Parameters.Add("@DefAmt", SqlDbType.VarChar);
cmd.Parameters.Add("@Freq", SqlDbType.VarChar);
cmd.Parameters.Add("@FundID", SqlDbType.Int);
cmd.Parameters.Add("@SeqID", SqlDbType.Int);
cmd.Parameters.Add("@FundPct", SqlDbType.VarChar);
cmd.Parameters.AddWithValue("@Today", System.DateTime.Now());
cmd.Parameters.AddWithValue("@begDt", "19000101");
cmd.Parameters.AddWithValue("@EndDT", "24000101");
conn.Open();
cmd.Connection = conn;
try {
//Loop through entire DataGridView
for (int i = 0; i <= SalaryDGV.Rows.Count - 1; i++) {
//If row is empty, exit For Next block.
if (BonusDGV.Rows(i).Cells(0).Value == null) {
break;
}
//Adds value to parameters from data grid view.
cmd.Parameters("@DefAmt").Value = SalaryDGV.Rows(i).Cells(0).Value;
cmd.Parameters("@Freq").Value = SalaryDGV.Rows(i).Cells(1).Value;
cmd.Parameters("@FundID").Value = Convert.ToInt32(SalaryDGV.Rows(i).Cells(2).Value);
cmd.Parameters("@SeqID").Value = SalaryDGV.Rows(i).Cells(3).Value;
cmd.Parameters("@FundPct").Value = SalaryDGV.Rows(i).Cells(4).Value;
cmd.ExecuteNonQuery();
}
} catch (Exception ex) {
MessageBox.Show("Error adding the Salary Deferral " + ex.Message);
}
Note: I used a converter so the code may not have translated the best.
Also note, I do not use this to insert 100's or thousands of rows of data, only 3-4 at a time. If there is a more efficient approach, I am not sure of it, however you asked me to post how I would do it.

Mark C.
- 6,332
- 4
- 35
- 71
-
-
-
-
Well as far as the most efficient I'm afraid I have never been in a scenario where my insert speed took a hit, so I am intrigued. – Mark C. May 28 '14 at 16:46
-
Its just what I read on the internet to be honest. A few people said it would work fine, others said it "takes too long" but when reading further it only took a few seconds anyway, which I could easily cope with. – Tom May 28 '14 at 16:48
-
-
-
Yes. This is assuming you have an existing table to save the data into. If you feel this solved your question please mark it as answered. – Mark C. Jun 15 '14 at 22:35
-
I wasn't very clear in the question, my mistake. I'd like to use the data to create a new table, instead of inserting into a existing table. – Tom Jun 15 '14 at 22:55
-
That's a different question. You'll have to start a new thread. Make sure you actually know what you're asking... – Mark C. Jun 16 '14 at 12:25