如何在visualc中用mysql中的数据填充组合框#

jxct1oxe  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(302)

我已经尝试了我在这里找到的每一个解决方案,但仍然无法让它发挥作用
目前我得到的只是一个空白的组合框,不管我改变了什么,有人能看到我的标题哪里出错了吗(但尝试价格由于成功与我的标题组合框)我试图拉标题和价格从我的游戏\u详细资料表在aliena\u商店mysql数据库。

  1. using System;
  2. using System.Windows.Forms;
  3. using MySql.Data.MySqlClient;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Data.Common;
  7. namespace Aliena_Store
  8. {
  9. public partial class Form2 : Form
  10. {
  11. public Form2()
  12. {
  13. InitializeComponent();
  14. }
  15. private void Form2_Load(object sender, EventArgs e)
  16. {
  17. }
  18. private void Game_List_SelectedIndexChanged(object sender, EventArgs e)
  19. {
  20. try
  21. {
  22. MySqlConnection connection = new MySqlConnection
  23. "server=localhost;user=root;database=Aliena_Store
  24. ;port=3306;password=Blackie");
  25. string selectQuery = "SELECT Title,Price FROM
  26. Aliena_Store.Game_Details";
  27. connection.Open();
  28. MySqlCommand command = new MySqlCommand(selectQuery, connection);
  29. MySqlDataReader reader = command.ExecuteReader();
  30. int i = 0;
  31. while (reader.Read())
  32. {
  33. Game_List.Items.Insert(i, reader.GetString("Title"));
  34. i++;
  35. }
  36. connection.Close();
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageBox.Show(ex.Message);
  41. }
  42. }
  43. private void Game_Quantity_TextChanged(object sender, EventArgs e)
  44. {
  45. }
  46. private void Game_Price_SelectedIndexChanged(object sender, EventArgs e)
  47. {
  48. }
  49. }
  50. }
ev7lccsx

ev7lccsx1#

您的sql可能没有返回您所想的内容
“选择标题*…”您可能不想在其中使用“*”。你得到的是一些空白条目还是没有条目?
如果您可以更改下拉列表中的项目,您知道您的代码正在运行吗?

ddarikpa

ddarikpa2#

如果您的dropdownlist是游戏列表,那么您应该尝试:

  1. int i =0
  2. while (reader.Read())
  3. {
  4. Game_List.Items.Insert(i, reader.GetString("Title"));
  5. i++
  6. }

相关问题