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

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

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

using System;
    using System.Windows.Forms;
    using MySql.Data.MySqlClient;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.Common;

    namespace Aliena_Store
     {
     public partial class Form2 : Form
     {

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }
    private void Game_List_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
            MySqlConnection connection = new MySqlConnection
        "server=localhost;user=root;database=Aliena_Store
        ;port=3306;password=Blackie");

            string selectQuery = "SELECT Title,Price FROM 
            Aliena_Store.Game_Details";
            connection.Open();
            MySqlCommand command = new MySqlCommand(selectQuery, connection);
            MySqlDataReader reader = command.ExecuteReader();
            int i = 0;
            while (reader.Read())
            {
                Game_List.Items.Insert(i, reader.GetString("Title"));
                i++;
             }
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }

    }

    private void Game_Quantity_TextChanged(object sender, EventArgs e)
    {

    }

    private void Game_Price_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    }
    }
ev7lccsx

ev7lccsx1#

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

ddarikpa

ddarikpa2#

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

int i =0
while (reader.Read())
{
    Game_List.Items.Insert(i, reader.GetString("Title"));
    i++
}

相关问题