I need to update a table column by applying a defined function to that column.
Using MySqlCommand for example.
Eg. I have a C# defined function that cleans text string GetCleanText(string text_to_clean)
and in one of my tables, I have a column fullTxt
that contains the text to be cleaned, and another column txt
that is empty for now.
I should have as a result: txt = GetCleanText(fullTxt)
.
I tried using a foreach
loop, but it is so expansive, since I have several rows in my table.
Here is my code:
// Get all entries in the log table that have the html log and do not have text answers
MySqlCommand request1 = new MySqlCommand("SELECT * FROM my_table WHERE fullTxt IS NOT NULL AND txt IS NULL", conn);
// loop over results and clean the text entries
List<List<string>> tobefilled = new List<List<string>>();
using (MySqlDataReader r = request1.ExecuteReader())
{
while (r.Read())
{
string id = r.GetString("id");
string fullTxt = r.GetString("fullTxt");
string txt = this.GetCleanText(fullTxt);
tobefilled.Add(new List<string>() { id, txt });
}
}
System.Console.WriteLine($"{tobefilled.Count} to be updated ...");
// Update all entries in the log table that have the html log and do not have text answers
MySqlCommand request2 = new MySqlCommand("UPDATE my_table SET txt = @txt WHERE id = @id", conn);
request2.Parameters.AddWithValue("@txt", "");
request2.Parameters.AddWithValue("@id", "");
foreach (List<string> elem in tobefilled)
{
request2.Parameters["@txt"].Value = elem[1];
request2.Parameters["@id"].Value = elem[0];
request2.ExecuteNonQuery();
}
I don't know if this is so clean as a method, but it works faster:
the resulting query, built with
StringBuilder
is too long, but it works.