C#返回数组对象

//返回数组对象

public List<User> GetUser()

        {
            try
            {
                using (SqlConnection testConnection = new SqlConnection(ConnStr.connectionString))
                {
                    SqlCommand testCommand = testConnection.CreateCommand();

                    testCommand.CommandText = "select [id],[name] from [user]";
                    
                    testConnection.Open();

                 
                    List<User> users = new List<User>();
                    SqlDataReader rd = testCommand.ExecuteReader();

                    while (rd.Read())//read next
                    {
                        User userback = new User();
                        userback.ID = rd.GetInt32(0);
                        userback.Name = rd.GetString(1);
                        users.Add(userback);    //add to list                    
                    }
                    
                    Console.WriteLine("Back all sql User");
                    return  users;
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.Message);
                return null;
            }

        }
posted @ 2010-04-13 19:00  邓维  阅读(6350)  评论(0编辑  收藏  举报