Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

mysql - How to display specific data in combobox coming from database in c#?

        private void stdName()
        {
            string MyConnection2 = "datasource=127.0.0.1;port=3306;username=root;password=;database=anecdotal;";
            MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
            MySqlDataReader row;
            MySqlCommand MyCommand2 = new MySqlCommand("SELECT  `ID`, `student_id`, `name` FROM student", MyConn2);
            try
            {
                MyConn2.Open();
                row = MyCommand2.ExecuteReader();
                while (row.Read())
                {               //Display student in combobox id_no student_id and student_name
                    std_detail.Items.Add(row.GetValue(0).ToString());
                    std_detail.Items.Add(row.GetValue(1).ToString());
                    std_detail.Items.Add(row.GetValue(2).ToString());

                }
                MyConn2.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

Here's my question;
1.)How can I ouput in as one row in my combobox i'm fetching the pk_id,student_id,student_name?
2.)Since in my code I was trying to output it one by one how can make it as one output coming from the database?

Currently in my code it output as a column instead of in one row only
I want an output like this (0,BL20201339,CRUZ JUANO) for each loop

question from:https://stackoverflow.com/questions/66045986/how-to-display-specific-data-in-combobox-coming-from-database-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Write it like below please:-

while (row.Read())
                {               
                    std_detail.Items.Add(string.Format("{0},{1},{2}",row.GetValue(0).ToString(),row.GetValue(1).ToString(),row.GetValue(2).ToString()));

                }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...