将listBox与数据库链接,如果在listBox中选择索引,则在textBox中显示值.C#

本文关键字:listBox textBox 则在 显示 索引 数据库 链接 如果 选择 | 更新日期: 2023-09-27 18:29:05

这适用于在列表框中显示数据库中的项目,但我需要,如果我在列表框选择了一个值,它会将有关该人的信息显示回文本框。那是我不能去上班的原因。

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);
            }
        }
    }
}

将listBox与数据库链接,如果在listBox中选择索引,则在textBox中显示值.C#

理想情况下,我会将您的列表框更改为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();
        }

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

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