C#和本地数据库(访问)

时间:2017-11-11 16:29:00

标签: c# ms-access

我需要帮助将listbox输出中的数据显示到标签。 在listbox中,所有内容都已加载(我需要查看FirstnameLastName),但它不会在标签上写任何内容 - 始终是错误。

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "SELECT * FROM Tab1 WHERE groupA='" + listBox1.Text + "' ORDER BY FirstName";
            command.CommandText = query;

            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                listBox3.Items.Add(reader["FirstName"].ToString() + " " + reader["LastName"].ToString());
            }

            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERROR" + ex);
        }
    }

listBox3_SelectedINdexChanged

private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = "select * from Tab1 where FirstName=" + listBox2.Text + "";
                command.CommandText = query;

OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { label6.Text = reader["FirstName"].ToString(); label8.Text = reader["LastName"].ToString(); label10.Text = reader["GroupPolice"].ToString(); } connection.Close(); } catch (Exception ex) { MessageBox.Show("ERROR" + ex); } }

谢谢。 enter image description here

1 个答案:

答案 0 :(得分:0)

将第二个查询更改为:

string query = "select * from [Tab1] where FirstName='" + listBox2.Text + "'";

while (reader.Read())来:

 if (reader.Read())