如何用visual c#中的mysql数据填充组合框

时间:2018-01-15 10:07:25

标签: c# mysql visual-studio combobox

我已经尝试了我在这里找到的每一个解决方案,但仍然无法让它工作

目前我得到的是一个空白的组合框,无论我改变什么,谁能看到我的标题出错? (但是由于我的标题组合框成功而试试价格) 我试图从Aliena_Store MySQL数据库中的Game_Details表中提取Title和Price。

    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)
    {

    }


    }
    }

2 个答案:

答案 0 :(得分:0)

如果您的下拉列表是Game_List,那么您应该尝试:

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

答案 1 :(得分:0)

您的SQL可能无法返回您的想法

“选择标题* ....”你可能不希望那里的“*”。您是获得了多个空白条目还是没有条目?

如果您可以更改右下角的项目,您确实知道您的代码正在运行吗?

相关问题