0

我有一个用于 ado.net 操作的基类,如下所示:

    public abstract class BaseDataAccess
    {
        private string _connectionString = ......

        protected string ConnectionString
        {
            get { return _connectionString; }
        }

        protected object ExecuteScalar(string spName, SqlParameter[] parameters=null)
        {
            using (SqlConnection connection = new SqlConnection(_connectionString))
            {
                using (SqlCommand command = new SqlCommand(spName, connection))
                {
                    if (parameters != null)
                    {
                        command.Parameters.AddRange(parameters);
                    }
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    connection.Open();
                    return command.ExecuteScalar();
                }
            }            
        }

        protected DataTable ExecuteReader(string spName, SqlParameter[] parameters=null)
        {
            using (SqlConnection connection = new SqlConnection(_connectionString))
            {
                using (SqlCommand command = new SqlCommand(spName, connection))
                {
                    if (parameters != null)
                    {
                        command.Parameters.AddRange(parameters);
                    }
                    command.CommandType = CommandType.StoredProcedure;
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        DataTable table = new DataTable();                        
                        table.Load(reader);
                        return table;
                    }
                }
            }
        }
    }

还有一个查询用户表的类

    public class UsersAccess : BaseDataAccess
    {
        private const string SP_COUNT_USERS = "spCountUsers";
        private const string SP_GET_USERS = "spGetUsers";

        public int CountUsers()
        {
            return (int)base.ExecuteScalar(SP_COUNT_USERS);
        }

        public IList<User> GetUsers()
        {
            DataTable usersTable = base.ExecuteReader(SP_GET_USERS);
            return GetUsersFromDataTable(usersTable);
        }

        private IList<User> GetUsersFromDataTable(DataTable usersTable)
        {
            IList<User> users = new List<User>();
            foreach (DataRow row in usersTable.Rows)
            {
                User user = new User();
                user.UserID = (int)row["UserID"];
                user.Password = (string)row["Password"];
                user.Username = (string)row["Username"];
            }
            return users;
        }
    }

数据读取器带有 2 行数据,但是当没有数据进入数据表时。如何做到这一点?

PS:为 ado.net 提供更好的基类的任何提示都会很棒。我只有很少的表,只有很少的程序/表。

4

1 回答 1

2

缺少users.Add(user);每个循环末尾的

    private IList<User> GetUsersFromDataTable(DataTable usersTable) 
    { 
        IList<User> users = new List<User>(); 
        foreach (DataRow row in usersTable.Rows) 
        { 
            User user = new User(); 
            user.UserID = (int)row["UserID"]; 
            user.Password = (string)row["Password"]; 
            user.Username = (string)row["Username"]; 
            users.Add(user);
        } 
        return users; 
    }
于 2012-08-09T20:53:36.693 回答