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");
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