1

我正在使用 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);  
    } 
4

1 回答 1

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);
        }
于 2012-02-24T20:07:47.683 回答