从MS Access数据库获取数据,并在一个列表框显示它 [英] Getting data from MS Access database and display it in a listbox

查看:162
本文介绍了从MS Access数据库获取数据,并在一个列表框显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阅读在MS Access数据库中的数据,并在一个列表框显示它。我有代码在这里,但我得到的错误。

How do I read data in ms access database and display it in a listbox. I have the codes here but i got errors.

 private void button3_Click(object sender, EventArgs e)
    {
        using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Sisc-stronghold\mis!\wilbert.beltran\DataBase\DataStructure.accdb"))
        using(OleDbCommand cmd = new OleDbCommand(" SELECT * from TableAcct", conn))
        {
            conn.Open();
            OleDbDataReader Reader = cmd.ExecuteReader();
            //if (Reader.HasRows)
            if (Reader.HasRows)
            {
                Reader.Read();
                listBox1.Text = Reader.GetString("FirstName");
            }
        } 



错误的位置:
1。错误1的最佳重载的方法匹配for'System.Data.Common.DbDataReader.GetString(INT)'有一些无效的参数。
2.错误2参数1:无法从字符串'到'廉政'

the errors are here: 1. Error 1 The best overloaded method match for'System.Data.Common.DbDataReader.GetString(int)' has some invalid arguments. 2. Error 2 Argument '1': cannot convert from 'string' to 'int'

推荐答案

试试这个转换之一,

       List<String> firstName = new List<String>();
       List<String> lastName = new List<String>();

       private void loadButton_Click(object sender, EventArgs e)
       {
                cn.Open();
                OleDbDataReader reader = null;
                cmd = new OleDbCommand("select* from Records", cn);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    firstName.Add(reader["FirstName"].ToString());
                    lastName.Add(reader["LastName"].ToString());
                }
                cn.Close();
       }



然后在您的搜索按钮,插入此,

then in your search button, insert this,

private void searchButton_Click(object sender, EventArgs e)
        {
            clearSearchResult();
            try
            {
                int totalItems = FirstName.Count;
                int count = 0;
                while (count < totalItems)
                {
                    if (textBox6.Text == FirstName[count].ToString())
                    {
                        listBox1.Items.Add(FirstName[count].ToString());
                        count = 100;
                    }
                    else
                    {
                        count++;
                    }



这是很好当你想显示<$ C的信息使用$ C>名字在 listBox1_SelectedIndexChanged 如果你想。这里有一个例子,

It's good to use when you want to show the information of the "FirstName" in the listBox1_SelectedIndexChanged if you want. here's an example,

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                int totalItems = lastName.Count;
                int count = 0;
                while (count < totalItems)
                {
                    if ((listBox1.SelectedItem.ToString()) == firstName[count].ToString()))
                    {
                        textBox1.Text = firstName[count].ToString();
                        textBox2.Text = lastName[count].ToString();
                        count = 100;
                    }
                    else
                    {
                        count++;
                    }
               }



希望这有助于

hope this helps,

这篇关于从MS Access数据库获取数据,并在一个列表框显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆