如何将数据从数据读取器读入数据表?

时间:2012-08-09 20:44:56

标签: c# ado.net

我有一个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;
                    }
                }
            }
        }
    }

查询users表的类

    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提供更好基类的任何提示都会很棒。我只有几张桌子,只有很少的程序/桌子。

1 个答案:

答案 0 :(得分: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; 
    }