mardi 3 septembre 2019

Is there a way to loop through database using a datacontext to find and replace a value?

I have a list of emailadresses that give a bumb if you send something to them, so they don't exist anymore. There a allot of them and it is unclear where they are in a huge database.

Is there a way to loop through the database, through a datacontext, not knowing the table name or column name, and still find and replace the value?

I already have a collection of table names and column names put together (if needed)

I have tried a bunch of stuff like using reflection and other things.

Dictionary<string, List<string>> tablesWithColumns = new Dictionary<string, List<string>>();
var model = new System.Data.Linq.Mapping.AttributeMappingSource().GetModel(typeof(SVDataContext));

foreach (System.Data.Linq.Mapping.MetaTable modelTable in model.GetTables())
{
    string tableName = modelTable.TableName;
    tablesWithColumns.Add(tableName, new List<string>());
    foreach (var dataMember in modelTable.RowType.DataMembers)
    {
        string columnName = dataMember.MappedName;
        tablesWithColumns[tableName].Add(columnName);
    }
}

Is there a way of using the following (pseudo):

string emailAdresThatHasToBeNull = "gmail@someemail.com";
using (DataContext dataContext = new DataContext())
{
    foreach (KeyValuePair<string, List<string>> keyValuePair in tablesWithColumns)
    {
         string tableName = keyValuePair.Key;
         foreach (string columnName in keyValuePair.Value)
         {
              var list = dataContext.(tableName).Columns(c => c.name == columnName).All(v => v == emailAdresThatHasToBeNull);
              foreach (var entry in list)
              {
                   entry = null;
              }
         }
     }
     dataContext.SubmitChanges();
}

The wanted result is everywhere the emailadres was is now NULL.

With kind regards, Ingmar





Aucun commentaire:

Enregistrer un commentaire