 | ADO.Net 2.0 Batch Update sample |
| // Get the DataTable with Rows State as RowState.Added
| | 1: DataTable dtInsertRows = new DataTable(); // GetDataTable();
| | 2:
| | 3: SqlConnection connection = new SqlConnection("connectionString");
| | 4: SqlCommand command = new SqlCommand("sp_BatchInsert", connection);
| | 5: command.CommandType = CommandType.StoredProcedure;
| | 6: command.UpdatedRowSource = UpdateRowSource.None;
| | 7:
| | 8: // Set the Parameter with appropriate Source Column Name
| | 9: command.Parameters.Add("@PersonId", SqlDbType.Int, 4, dtInsertRows.Columns[0].ColumnName);
| | 10: command.Parameters.Add("@PersonName", SqlDbType.VarChar, 100, dtInsertRows.Columns[1].ColumnName);
| | 11: SqlDataAdapter adpt = new SqlDataAdapter();
| | 12: adpt.InsertCommand = command;
| | 13:
| | 14: // Specify the number of records to be Inserted/Updated in one go. Default is 1.
| | 15: adpt.UpdateBatchSize = 20;
| | 16: connection.Open();
| | 17: int recordsInserted = adpt.Update(dtInsertRows);
| | 18: connection.Close();
| | 19: |
|