使用列表从数据库接收行

Using a list to receive rows from a database

我正在尝试在连接数据库时填充列表,我已经正确地关闭了 MVVM,但我不知道如何定义列表,然后在该列表中连接到数据库并填充字段。谁能帮忙?我的数据库工作正常...

UserRepository

public class UserRepository: IUserRepository
{

    private System.Data.SqlClient.SqlConnection connectionToDB;

    private System.Data.SqlClient.SqlDataAdapter dataAdapter;

    // Get the data set generated by the sqlStatement
    public System.Data.DataSet getDataSet(string sqlStatement)
    {
        System.Data.DataSet dataSet;

        // creates the object dataAdapter to manipulate a table from the database
        dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlStatement, connectionToDB);

        // create the dataset
        dataSet = new System.Data.DataSet();

        dataAdapter.Fill(dataSet);

        //return the dataSet
        return dataSet;
    }
    public List<Users> GetUsers()
    {
        List<Users> users;


        // Database connection to go here along with the list population
        // Something like this?
        //
        // new User
        //     ID = 
        //     FirstName = 
        //     LastName =   

        //  Users = CollectionViewSource.GetDefaultView(_users);



        return users;
    }


}

DBViewModel

public class DBDisplayViewModel
{
    private ObservableCollection<Users> users;

    public DBDisplayViewModel()
    {
        Load();
    }

    public IUserRepository UserRepository
    {
        get; set;
    }

    public ObservableCollection<Users> Users
    {
        get
        {
            if (users == null)
            {
                users = new ObservableCollection<Users>();
            }

            return users;
        }
        set
        {
            if (value != null)
            {
                users = value;
            }
        }
    }

    private void Load()
    {
        List<Users> users= UserRepository.GetUsers();
        Users= new ObservableCollection<Users>(users);
    }
}

Users

这只是定义了 ID FirstName LastName 属性

所以分解一下:需要创建 UserRepository 中注释掉的信息以拉入数据库连接和数据库值。谁能帮忙?太有爱了!

看下面的代码,会有思路

        public List<Users> GetUsers()
            {
                List<Users> users=new  List<Users> ();
                DataSet ds=getDataSet("Select FirstName,...... from Users")
                Users user;

                 foreach(DataRow row in ds.Tables[0].Rows)
                 {
                   user=new Users();
                   user.FirstName=row["firstname"].ToString();
                    ....
                    ....
                   users.Add(user)
                 }
                return users;
            }