public static void AttachNewTable(SqlConnection connection, DataTable table, string tableName) { if (connection.State == ConnectionState.Closed) connection.Open(); DataTable cloneTable = table.Clone(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.DestinationTableName = tableName; bulkCopy.WriteToServer(cloneTable); } connection.Close(); } // Usage AttachNewTable(con, yourDataTable, "YourTable");
private void AttachTableToDataset(DataTable dataTable, DataSet dataSet) { dataTable.TableName = "YourTableName"; dataSet.Tables.Add(dataTable); dataSet.AcceptChanges(); } // Usage AttachTableToDataset(yourDataTable, yourDataSet);This code example shows how to attach a new DataTable to an existing DataSet using ITable Attach method. It adds the specified table to the DataSet and accepts any changes made to it. Package Library: System.Data