使用Access数据库的基本Windows窗体,数据库项文本不在列表框中显示

时间:2018-10-18 16:14:06

标签: c# database ms-access

我正在使用Access数据库中的简单数据集,并学习如何使用Visual Studio将其与C#连接并将数据项显示在表单上的列表框中。连接成功,并且似乎正在读取项目,因为一旦单击加载数据的按钮,在列表框中,我就可以看到突出显示的部分,好像那里有不可见的文本,并且可以滚动浏览但不能看到任何文字吗?任何意见,将不胜感激。 What I am seeing on my form.

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 System.Data.OleDb;    //Data Access Library for MS Access

namespace DbAccessApp
{
    public partial class FormMember : Form
    {
        //Class Level Objects
        private OleDbConnection dbConn;     //Connection Object
        private OleDbCommand dbCmd;         //Command Object
        private OleDbDataReader dbReader;   //Reader Object
        private Member aMember;             //One of Our Members
        private string sConnection;         //Connection String
        private string sql;                 //SQL Command

        public FormMember()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                sConnection =
                    "Provider=Microsoft.ACE.OLEDB.12.0;" +  //Connection Provider
                    "Data Source=Data\\MemberDB.accdb";      //Database Source
                dbConn = new OleDbConnection(sConnection);
                dbConn.Open();

                sql = "SELECT * FROM memberTable " +
                    "ORDER BY LastName ASC, FirstName ASC;";

                //Create our command object
                dbCmd = new OleDbCommand();
                dbCmd.CommandText = sql;

                //Tell the command object which connection to use
                dbCmd.Connection = dbConn;

                //Create our Reader Object
                dbReader = dbCmd.ExecuteReader();   //Instantiates reader and loads with data

                while (dbReader.Read())//Run as long as I can read from my reader
                {
                    aMember = new Member(
                        dbReader["FirstName"].ToString(),
                        dbReader["LastName"].ToString());
                    lstBxMembers.Items.Add(aMember);
                }//end of while

                lblMessage.Text = "Members Loaded";
                dbReader.Close();   //Close reader
                dbConn.Close();     //Close connection
            }//end of try
            catch (System.Exception exc)
            {
                lblMessage.Text = exc.Message;
            }//end of catch

        }//end of btn_click

    }//end of class
}//end of main

这是我的会员班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbAccessApp
{
    class Member
    {
        #region -- Class Fields
        private string id;
        private string firstName;
        private string lastName;
        private string phoneNumber;
        #endregion -- Class Fields

        #region -- Public Properties
        public string Id { get => id; }
        public string FirstName { get => firstName; }
        public string LastName { get => lastName; }
        public string PhoneNumber { get => phoneNumber; }
        #endregion -- Public Properties

        #region -- Constructors
        /// <summary>
        /// Default No Arg
        /// </summary>
        public Member()
        {

        }

        public Member(string fName, string lName)
        {

        }
        #endregion Constructors

        #region -- Methods
        public override string ToString()
        {
            return firstName + " " + lastName;
        }
        #endregion -- Methods
    }
}

0 个答案:

没有答案