Advertisement
Genral

Untitled

Apr 27th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. public void Load_Acc()
  2. {
  3. string query = "SELECT id, Username, Password, Mobile, Email, LastCheck, State, F_Name FROM accounts;";
  4. Dictionary<int, Dictionary<string, object>> userDataDictionary = new Dictionary<int, Dictionary<string, object>>();
  5. try
  6. {
  7. using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(Program.connection("db_sql", "")))
  8. {
  9. connection.Open();
  10. using (MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(query, connection))
  11. {
  12. using (MySql.Data.MySqlClient.MySqlDataReader reader = command.ExecuteReader())
  13. {
  14. int rowIndex = 0;
  15. while (reader.Read())
  16. {
  17. Dictionary<string, object> rowData = new Dictionary<string, object>();
  18. rowData["id"] = reader["id"];
  19. rowData["Username"] = reader["Username"];
  20. rowData["Password"] = reader["Password"];
  21. rowData["Mobile"] = reader["Mobile"];
  22. rowData["Email"] = reader["Email"];
  23. rowData["LastCheck"] = reader["LastCheck"];
  24. rowData["State"] = reader["State"];
  25. rowData["F_Name"] = reader["F_Name"];
  26. userDataDictionary[rowIndex] = rowData;
  27. rowIndex++;
  28. }
  29. }
  30. }
  31. }
  32. dataGridView1.DataSource = ConvertToDataTable(userDataDictionary).DefaultView;
  33. }
  34. catch (Exception ex)
  35. {
  36. MessageBox.Show("An error occurred: " + ex.Message);
  37. }
  38. }
  39. private DataTable ConvertToDataTable(Dictionary<int, Dictionary<string, object>> dictionary)
  40. {
  41. DataTable table = new DataTable();
  42.  
  43. foreach (var innerDictionary in dictionary.Values)
  44. {
  45. if (table.Columns.Count == 0)
  46. {
  47. foreach (var columnName in innerDictionary.Keys)
  48. {
  49. table.Columns.Add(columnName);
  50. }
  51. }
  52.  
  53. DataRow row = table.NewRow();
  54. foreach (var keyValuePair in innerDictionary)
  55. {
  56. row[keyValuePair.Key] = keyValuePair.Value;
  57. }
  58. table.Rows.Add(row);
  59. }
  60.  
  61. return table;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement