Hi Dear,
Please see this code.
Here Use Two function
(1) FxInsert
(2) FxUpdate
FxInsert : Description how to insert data in SQL server Database.
FxUpdate : Description how to Update data in SQL server Database.
Function :
FxInsert
private void FxInsert()
{
for (int i = 0; i < dataGridView2.RowCount - 1; i++)
{
string sql = "INSERT INTO [SAPProduction].[ProductionData] ( [Jobcard],[Planqty],[Mouldqty],[Operator],[Supervisor],[Profilename],[Process],[Date],[Remark],[Approvedby],[id],[Division],[Od]) " +
"
VALUES('" + dataGridView2.Rows[i].Cells[" Jobcard]"].Value.ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Planqty]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Mouldqty]"].ToString() + "' " +
"'" + dataGridView2.Rows[i].Cells["[Operator]"].ToString() + "' " +
"'" + dataGridView2.Rows[i].Cells["[Supervisor]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Profilename]"].ToString() + "'" +
" '" + dataGridView2.Rows[i].Cells["[Process]"].ToString() + "'" +
" '" + dataGridView2.Rows[i].Cells["[Date]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Remark]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Approvedby]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[id]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Division]"].ToString() + "' " +
" '" + dataGridView2.Rows[i].Cells["[Od]"].ToString() + "' ) ";
SqlCommand cmd = new SqlCommand(sql, objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
}
}
--------------------------------------------------------------
Function :
FxUpdate
private void FxUpdate()
{
for (int i = 0; i < dataGridView2.RowCount - 1; i++)
{
string sql = "UPDATE [SAPProduction].[ProductionData] SET " +
" [Jobcard]= '" + dataGridView2.Rows[i].Cells["[Jobcard]"].Value.ToString() + "' , " +
" [Planqty]= '" + dataGridView2.Rows[i].Cells["[Planqty]"].ToString() + "', " +
" [Mouldqty] = '" + dataGridView2.Rows[i].Cells["[Mouldqty]"].ToString() + "', " +
" [Operator] = '" + dataGridView2.Rows[i].Cells["[Operator]"].ToString() + "', " +
" [Supervisor] = '" + dataGridView2.Rows[i].Cells["[Supervisor]"].ToString() + "' , " +
" [Profilename] ='" + dataGridView2.Rows[i].Cells["[Profilename]"].ToString() + "' , " +
" [Process]='" + dataGridView2.Rows[i].Cells["[Process]"].ToString() + "' , " +
" [Date]='" + dataGridView2.Rows[i].Cells["[Date]"].ToString() + "' , " +
" [Remark]='" + dataGridView2.Rows[i].Cells["[Remark]"].ToString() + "' , " +
" [Approvedby]='" + dataGridView2.Rows[i].Cells["[Approvedby]"].ToString() + "' , " +
" [id]='" + dataGridView2.Rows[i].Cells["[id]"].ToString() + "' , " +
" [Division]='" + dataGridView2.Rows[i].Cells["[Division]"].ToString() + "' ";
SqlCommand cmd = new SqlCommand(sql, objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
}
}