2

I am creating a Attendance System and using grid view to insert the data. There may be many rows on the grid. All things are going well and data are also entering well. But I am using a for loop to check each row. This make the performance quite slow when the number of rows increases. And also the round trips increases with the growing number of rows.

Can anyone provide a better solution for this?

I have modify my CODE according to u all.....but now a problem has arise it is only inserting the last row of the grid multiple times......Other than this the Code is fine.

MySqlDataAdapter myworkdatta = myworkdatta = new MySqlDataAdapter("SELECT CID,EID,TID,ATTENDENCE FROM EMPLOYEEATT ORDER BY AID DESC LIMIT 1", conn);
                                    DataSet myworkdsatt = new DataSet();
                                    myworkdatta.Fill(myworkdsatt, "EMPLOYEEATT");

                                    int i;
                                    for (i = 0; i < emplist_gv.Rows.Count; i++)
                                    {
                                        string tid = emplist_gv.Rows[i].Cells[6].Value.ToString();
                                        string eid = emplist_gv.Rows[i].Cells[0].Value.ToString();
                                        string atid = emplist_gv.Rows[i].Cells[7].Value.ToString();

                                        MySqlCommand cmdwk = new MySqlCommand("INSERT INTO EMPLOYEEATT (CID,EID,TID,ATTENDENCE) VALUES (@cid,@eid,@tid,@attendence)", conn);
                                        MySqlParameter spcidatt = new MySqlParameter("@cid", calid);
                                        MySqlParameter speid = new MySqlParameter("@eid", eid);
                                        MySqlParameter sptid = new MySqlParameter("@tid", tid);
                                        MySqlParameter spattendence = new MySqlParameter("@attendence", atid);

                                        cmdwk.Parameters.Add(spcidatt);
                                        cmdwk.Parameters.Add(speid);
                                        cmdwk.Parameters.Add(sptid);
                                        cmdwk.Parameters.Add(spattendence);

                                        myworkdatta.InsertCommand = cmdwk;

                                        DataRow drowk = myworkdsatt.Tables["EMPLOYEEATT"].NewRow();
                                        drowk["CID"] = calid;
                                        drowk["EID"] = eid;
                                        drowk["TID"] = tid;
                                        drowk["ATTENDENCE"] = atid;

                                        myworkdsatt.Tables["EMPLOYEEATT"].Rows.Add(drowk);

                                    }

                                    myworkdatta.Update(myworkdsatt, "EMPLOYEEATT");
10
  • Move selects before loop, make changes of table inside loop and update table after loop. Selecting 2 times and inserting data during one iteration of loop is not good idea. Commented Dec 5, 2011 at 7:18
  • Select Statement is needed because it will be fetching the last row ID and increment by 1 with the regular exp. Commented Dec 5, 2011 at 14:31
  • I have modify my CODE according to u .....but now a problem has arise it is only inserting the last row of the grid multiple times......Other than this the Code is fine. Commented Dec 5, 2011 at 19:12
  • you need to get id before loop and increment it in each iteration. Something like drowk["ATTENDENCE"] = atid + i; Commented Dec 5, 2011 at 21:05
  • the ID is auto_incremented so its happening on the Database itself... Commented Dec 6, 2011 at 17:34

2 Answers 2

1

Considering your 2 select SQL statement doesn't seem to contain anything relevant to the the specific row you can take that out of the loop and just use its values easy enough.

Because you need to do an insert on each row, which I don't understand why, then it seems hard to remove the database hits there.

If you are doing a bulk insert you could look at bulk inserts for MySql: MySql Bulk insert

Sign up to request clarification or add additional context in comments.

9 Comments

Sir can't this rows be stored in the Datatable temp. and later updated to the Database...
Sir the Select Statement is needed because i will be fetching the last row ID and increment by 1 with the regular exp.
You could store it in a file and run a batch process in the background if you wanted. It couldn't be stored in memory or anywhere so there will still be some disk writing going on. Probably a lot faster than DB writing.
With getting the last row you can use mysql_insert_id() in the same insert statement and get the ID back with the insert. As for incrementing the ID by 1, if you are doing this manually it should be done in the database by setting the primary key as an identity column
I have modify my CODE according to u .....but now a problem has arise it is only inserting the last row of the grid multiple times......Other than this the Code is fine.
|
0

You can use SqlBulkCopy, it's easy to use. Basically just provide it with a data table (or data reader) and it will copy the rows from that source to your destination table.

Shortly, the code block would look like:

DataTable dataTableInGridView = (DataTable)emplist_gv.DataSource;
using (SqlConnection connection =
            new SqlConnection(connectionString))
{
    using (SqlBulkCopy bulkCopy =
                    new SqlBulkCopy(connection))
    {
        bulkCopy.DestinationTableName =
            "dbo.BulkCopyDemoMatchingColumns";
        try
        {
            // Write from the source to the destination.
            bulkCopy.WriteToServer(dataTableInGridView);
        }
        catch (Exception ex)
        {
            // Handle exception
        }
    }
}

1 Comment

SqlBulkCopy is not Supported in MySQl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.