Instead of passing an array of names and an array of values, you can pass a Dictionary<string, object> , which contains the parameter names for the keys and arbitrary objects (in your case, also strings) for the values ββyou want to insert in your query.
Each element contains a string with the name of the parameter and the value to be replaced. They are related to what are known as key-value pairs, and help to greatly simplify the mapping of names and values ββfor your request.
Something like that:
var parameters = new Dictionary<string, object> { { "?FirstName", "Josef" }, { "?LastName", "Stalin" } }; MyConnection.MySqlLink(strConnection, strCommand, parameters, dtTable);
Here you can see that using the dictionary is easier than managing two separate collections of parameters and values, respectively.
Inside your method, use a foreach loop with KeyValuePair<TKey, TValue> instead of a for loop with numeric indices, for example:
public static void MySqlLink(string strConnection, string strCommand, Dictionary<string, object> parameters, DataTable dtTable) { dtTable.Clear(); MySqlConnection MyConnection = new MySqlConnection(strConnection); MySqlCommand MyCommand = new MySqlCommand(strCommand, MyConnection); foreach (KeyValuePair<string, object> param in parameters) { MyCommand.Parameters.AddWithValue(param.Key, param.Value); } MySqlDataAdapter MyDataAdapter = new MySqlDataAdapter(MyCommand); MyDataAdapter.Fill(dtTable); }
source share