我需要通过将定义的函数应用于该列来更新表列。以 MySqlCommand 为例。例如。我有一个 C# 定义的函数来清理文本string GetCleanText(string text_to_clean),在我的一个表中,我有一列fullTxt包含要清理的文本,另一列txt现在是空的。
结果我应该有:txt = GetCleanText(fullTxt)。
我尝试使用foreach循环,但它是如此广泛,因为我的表中有几行。
这是我的代码:
// 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();
}