将用户从Access读入列表只返回一个用户

时间:2012-02-24 19:44:30

标签: c# wpf list ms-access oledbdatareader

我正在使用VS 2010 SP1在C#中开发WPF应用程序。

我在网上和其他地方看了很多例子,我的代码看起来是正确的,但是当我尝试从Access DB读取行到一个类然后进入一个ObservableCollection时(我也试过了)只是一个List),只有一个结束于集合中。然后我尝试在ListBox中显示用户名列表,当然只显示集合中的一个用户。

我已经坚持了一段时间了。任何帮助将不胜感激。我希望这只是一件非常微不足道的事情,我只是在俯视。

用户类:

public class User
{
    public User() { }
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string UserTitle { get; set; }
    public string UserArea { get; set; }
}

代码:

// Setting up DB stuff.
string s_ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data " + 
    "Source=|DataDirectory|\\CloseoutApp.accdb";
OleDbConnection AccessConn = new OleDbConnection(s_ConnString);

string s_Query = "SELECT UserID, UserName, UserTitle, UserArea FROM Users " +
    "ORDER BY UserID;";

OleDbCommand AccessCmd = new OleDbCommand(s_Query, AccessConn);
OleDbDataReader rdr;

AccessConn.Open();
rdr = AccessCmd.ExecuteReader();

// Collection of Users.
ObservableCollection<User> userList = new ObservableCollection<User>();

try
        {
            // Read each user from DB into a User instance, then add that instance to the userList.
            while(rdr.Read())
            {
                User newUser = new User();
                newUser.UserID = rdr.GetInt32(0);
                newUser.UserName = rdr.GetString(1);
                newUser.UserTitle = rdr.GetString(2);
                newUser.UserArea = rdr.GetString(3);

                userList.Add(newUser);
            }

        }

catch(Exception e)
        {
            //Update Statusbar
        }

// Close the DB connection.
rdr.Close();
AccessConn.Close();

// Add users to the ListBox.
foreach(User u in userList)
    {
        lb_Users.Items.Add(u.UserName);  
    } 

1 个答案:

答案 0 :(得分:0)

当我以类似的方式处理数据库连接时,我遇到了类似的事情。

尝试下面的代码:

// Setting up DB stuff.
        string s_ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data " +
            "Source=|DataDirectory|\\CloseoutApp.accdb";

        string s_Query = "SELECT UserID, UserName, UserTitle, UserArea FROM Users " +
            "ORDER BY UserID;";

        ObservableCollection<User> userList = new ObservableCollection<User>();

        using (OleDbConnection AccessConn = new OleDbConnection(s_ConnString))
        {
            using (OleDbCommand AccessCmd = AccessConn.CreateCommand())
            {
                AccessCmd.CommandText = s_Query;

                try
                {
                    AccessConn.Open();

                    OleDbDataReader rdr = AccessCmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        User newUser = new User();
                        newUser.UserID = rdr.GetInt32(0);
                        newUser.UserName = rdr.GetValue(1).ToString();
                        newUser.UserTitle = rdr.GetValue(2).ToString();
                        newUser.UserArea = rdr.GetValue(3).ToString();

                        userList.Add(newUser);
                    }

                    rdr.Close();

                }
                catch(Exception ex)
                {
                    //do something with ex
                }

            }

        }

        // Add users to the ListBox.
        foreach (User u in userList)
        {
            lb_Users.Items.Add(u.UserName);
        }