Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用mysqllist填充Listbox_C#_Mysql_Listbox - Fatal编程技术网

C# 用mysqllist填充Listbox

C# 用mysqllist填充Listbox,c#,mysql,listbox,C#,Mysql,Listbox,这是我的密码 我试图从mysql表中获取一个特定的行到我的列表框 有人可以告诉我为什么所有的工作正常,但列表框中没有显示任何项目? 清除列表框的代码也不起作用。。。 有什么想法吗 string MyConString = "SERVER=" + ip + ";" + "DATABASE=" + db + ";" + "UID=" + user + ";" + "PASSWORD=" + pw + ";"; MySqlConnection connection =

这是我的密码 我试图从mysql表中获取一个特定的行到我的列表框 有人可以告诉我为什么所有的工作正常,但列表框中没有显示任何项目? 清除列表框的代码也不起作用。。。 有什么想法吗

        string MyConString = "SERVER=" + ip + ";" + "DATABASE=" + db + ";" + "UID=" + user + ";" + "PASSWORD=" + pw + ";";
        MySqlConnection connection = new MySqlConnection(MyConString);
        string column = (string)comboBox1.SelectedValue;
        string query = "SELECT * from t_string;";

        List<string>[] list = new List<string>[1];
        list[0] = new List<string>();

        connection.Open();
        //Create Command
        MySqlCommand cmd = new MySqlCommand(query, connection);
        //Create a data reader and Execute the command
        MySqlDataReader dataReader = cmd.ExecuteReader();

        //Read the data and store them in the list
        while (dataReader.Read())
        {
            list[0].Add(dataReader[column] + "");
        }

        MessageBox.Show("Starting");
        Form1 form1 = new Form1();
        //Clear Listbox
        form1.listBox1.Items.Clear();
        // Add List to Textbox
        form1.listBox1.Items.AddRange(list);
        form1.listBox1.Refresh();
        MessageBox.Show("Finished");

        //close Data Reader
        dataReader.Close();

因为我不会在这里发布一个问题来获得任何其他人的答案,如果没有我自己的工作,我自己会得出一个答案:p

我使用一个DataTable和一个类来填充组合框

            string MyConString = "SERVER=" + Config_Settings.ip + ";" + "DATABASE=" + Config_Settings.db + ";" + "UID=" + Config_Settings.user + ";" + "PASSWORD=" + Config_Settings.pw + ";";
            MySqlConnection connection = new MySqlConnection(MyConString);
            string command = "SHOW COLUMNS FROM t_string";
            MySqlDataAdapter da = new MySqlDataAdapter(command, connection);
            DataTable dt = new DataTable();
            da.Fill(dt);

            List<MyModel> models = new List<MyModel>();
            foreach (DataRow row in dt.Rows)
            {
                MyModel model = new MyModel
                {
                    Name = (string)row[0]
                };
                models.Add(model);
            }

            bindingSource1.DataSource = models;
            comboBox1.DataSource = bindingSource1.DataSource;
            comboBox1.DisplayMember = "Languae";
            comboBox1.ValueMember = "Languae";

您是否收到任何异常或错误消息?您是否一步一步地调试了代码?没有错误或异常,应用程序仍然运行良好。@Sajeetharan winforms抱歉,没有发布itform1.listBox1.Items.AddRangelist;这不是form1.listBox1.Items.AddRangelist[0]?您知道您实际读取了多少数据吗?您是指数据库中还是列表[0]中有5000多行数据?
    public class MyModel
    {
        public string Name { get; set; }
    }