I'm writing a C# application that will synchronize data from a remote database to my MySQL web application, and am in need of advice on how to do this better.
In my C# app, I establish a connection to the remote MySQL database and pass that connection to a function which runs a loop to pass a list of queries, one in each iteration, to another function that executes each query. The way it is written works great, but it seems to be counter-productive with the use of the foreach loop and the IEnumerable function. I'm not sure if the way it is written now makes sense and I would like to know if I can write this without the foreach loop, while keeping the IEnumerable function, if possible.
private void UpdateRecords(MySqlConnection connection) {
foreach (string query in UpdateQueries(connection, _queries)) { }
}
private IEnumerable<string> UpdateQueries(MySqlConnection connection, List<string> queries) {
List<string>.Enumerator enumerator = queries.GetEnumerator();
while (enumerator.MoveNext()) {
MySqlCommand command = new MySqlCommand();
command.Connection = connection;
command.CommandText = enumerator.Current;
command.ExecuteNonQuery();
yield return enumerator.Current;
}
}
EDIT: Added 4 lines to the UpdateQueries function describing the use of the MySQL connection in this function.
UpdateQueries? Why not just ditch that whole thing and just doforeach (var query in _queries) { }?UpdateQueries, theUpdateRecordscode just seems equivalent toforeach (string query in _queries) { /* proccess query */ }UpdateQueries. See answer below on how you could better structure this.