如果在listBox中选择索引,则将listBox链接到Database并在textBox中显示值。 C#

时间:2013-08-27 10:41:41

标签: c# mysql

这适用于在列表框中显示数据库中的项目,但我需要如果我在列表框中选择一个值,它会将有关该人员的信息显示回文本框。那是我无法工作的事情。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace program
{
    public partial class Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();
            fill_listbox();
        }

        void fill_listbox()
        {
            string constring = "datasource=sql2.freesqldatabase.com;port=3306;username=sql217040;password=xxxxx";
            string Query = "select * from sql217040.fakedata ;";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                while (myReader.Read())
                {
                    string id1 = myReader.GetString("id");
                    string name1 = myReader.GetString("name");
                    string surname1 = myReader.GetString("surname");
                    listBox1.Items.Add(id1 + ' ' + name1 + ' ' + surname1);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "datasource=sql2.freesqldatabase.com;port=3306;username=sql217040;password=xxxxx";
            string Query = "select * from sql217040.fakedata where name='" + listBox1.Text + "' ;";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                while (myReader.Read())
                {

                    string sname = myReader.GetString("name");
                    string ssurname = myReader.GetString("surname");
                    string sphone = myReader.GetString("phone");
                    textBox1.Text = sname;
                    textBox2.Text = ssurname;
                    //telephone.Text = sphone;

                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

    }
}

2 个答案:

答案 0 :(得分:0)

理想情况下,我会将您的列表框更改为datagridview,并像这样更改您的方法

void fill_listbox()
    {
       try
        {
        string constring = "datasource=sql2.freesqldatabase.com;port=3306;username=sql217040;password=xxxxx";
        string Query = "select * from sql217040.fakedata ;";
        using(MySqlConnection conDataBase = new MySqlConnection(constring))
        {
            conDataBase.Open();
            using (SqlDataAdapter a = new SqlDataAdapter(Query, conDataBase))
            {
                DataTable t = new DataTable();
                a.Fill(t);          
                // Render data onto the screen
                dataGridView1.DataSource = t; // <-- datagridview1 is the gridview i have added
         }
       }
       }
       catch(Exception ex)
       {
            MessageBox.Show(ex.ToString());
       }
    }

并从gridview的单元格双击事件中读取这样的值

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string name = dataGridView1.Rows[e.RowIndex].Cells["columnname"].Value.ToString();
        }

答案 1 :(得分:0)

为什么不使用DataSet而不是DataReader。

只需将所有项目添加到列表框中,然后通过listbox selectedItemIndex从DataSet中获取所选项目。当列表框选择的索引发生变化时,这将保存额外的查询。