日期:2014-05-17  浏览次数:20974 次

菜鸟提问:SqlDateReader怎么读取多行记录?
我写的C#代码如下:

List<Ex_User> exUserList = new List<Ex_User>();
Ex_User exUser = new Ex_User();

string strSql="select ID,Name,LOGINNAME,PASSWORD,CGROUP,VALIDSTATUS from UM_User";

string strConn=@"Data Source=.;Initial Catalog=usermanagement;User  Id=sa;Password=111";
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sbSql.ToString();
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            exUser.ID = Convert.ToInt64(reader["id"]);
                            exUser.NAME = reader["NAME"].ToString();
                            exUser.LOGINNAME = reader["LoginName"].ToString();
                            exUser.CGROUP = (int)reader["cGroup"];
                            exUser.VALIDSTATUS = (int)reader["ValidStatus"];
                            exUser.PASSWORD = reader["Password"].ToString();
                            exUserList.Add(exUser);
                        }
                    }
              &