现在同学们都学到Y2了!!DBHelper类也用来越来越多了!!
经常在数据访问层的一个方法里面就就要反复的调用用dataRead.read()这个方法!!
由于我们的DBHelper是静态类!所以我们调用的是一个Read!当第二次调用的时候就抛出“阅读器关闭时MetaData 的尝试无效”这样的错误!!
看代码:public static List<Book> GetAllBook()
        {
            List<Book> books = new List<Book>();
            string sql = "select * from books";
           SqlDataReader reader = DBHelper.GetDataReader(sql))        
             while (reader.Read())
                {
                    Book book = new Book();
                    book.Id = Convert.ToInt32(reader["id"]);
                    book.Title = reader["Title"].ToString();
                    book.Author = reader["Author"].ToString();
                    book.PublishDate = reader["PublishDate"].ToString();
                    book.ISBN = reader["ISBN"].ToString();
                    book.WordsCount = Convert.ToInt32(reader["WordsCount"]);
                    book.UnitPrice = Convert.ToDouble(reader["UnitPrice"]);
                    book.ContentDescription = reader["ContentDescription"].ToString();
                    book.AurhorDescription = reader["AurhorDescription"].ToString();
                    book.EditorComment = reader["EditorComment"].ToString();
                    book.TOC = reader["toc"].ToString();
                    book.Clicks = Convert.ToInt32(reader["Clicks"]);        
                    
                    book.Publisher = BookPublisherServer.GetPublisherById(Convert.ToInt32(reader["publisherId"]));
                    int test = Convert.ToInt32(reader["publisherId"]);
                    Console.WriteLine(test);
                    book.Category = BookCategoryService.CetCategorById(test);
                    books.Add(book);
                }
                reader.Close();
                return books;
            }GetPublisherById方法如下:public static Publisher GetPublisherById(int publisherId)
        {
            string sql = "select * from publishers where id = @id";
            SqlDataReader reader = DBHelper.GetDataReader(sql, new SqlParameter("@id", publisherId)           
                if (reader.Read())
                {
                    Publisher publisher = new Publisher();
                    publisher.Id = Convert.ToInt32(reader["id"]);
                    publisher.Name = reader["name"].ToString();
                    reader.Close();
                    return publisher;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
   
当代码运行到 book.Publisher =...这里的时候就抛出了异常这个异常!
因为在这句里面调用了GetPublisherById()这个方法!!
而在GetPublisherById()这个方法里面把read给关掉了,所以Convert.ToInt32(reader["publisherId"])就把异常抛出了!!
同学们看到这的时候就会有人想到说GetPublisherById方法里面的read不关呀!!但是不关GetPublisherById这个方法又一直占用着read!也不行!!
正好这时候我们的using就可以出场了!!
我们可以把上面的代码改一下如下:
public static List<Book> GetAllBook()
        {
            List<Book> books = new List<Book>();
            string sql = "select * from books";
            using (SqlDataReader reader = DBHelper.GetDataReader(sql))
            {
                while (reader.Read())
                {
                    Book book = new Book();
                    book.Id = Convert.ToInt32(reader["id"]);
                    book.Title = reader["Title"].ToString();
                    book.Author = reader["Author"].ToString();
                    book.PublishDate = reader["PublishDate"].ToString();
                    book.ISBN = reader["ISBN"].ToString();
                    book.WordsCount = Convert.ToInt32(reader["WordsCount"]);
                    book.UnitPrice = Convert.ToDouble(reader["UnitPrice"]);
                    book.ContentDescription = reader["ContentDescription"].ToString();
                    book.AurhorDescription = reader["AurhorDescription"].ToString();
                    book.EditorComment = reader["EditorComment"].ToString();
                    book.TOC = reader["toc"].ToString();
                    book.Clicks = Convert.ToInt32(reader["Clicks"]);        
                    
                    book.Publisher = BookPublisherServer.GetPublisherById(Convert.ToInt32(reader["publisherId"]));
                    int test = Convert.ToInt32(reader["publisherId"]);
                    Console.WriteLine(test);
                    book.Category = BookCategoryService.CetCategorById(test);
                    books.Add(book);
                }
                     return books;
            }
        }GetPublisherById方法:
public static Publisher GetPublisherById(int publisherId)
        {
            string sql = "select * from publishers where id = @id";
            using (SqlDataReader reader = DBHelper.GetDataReader(sql, new SqlParameter("@id", publisherId)))
            {
                if (reader.Read())
                {
                    Publisher publisher = new Publisher();
                    publisher.Id = Convert.ToInt32(reader["id"]);
                    publisher.Name = reader["name"].ToString();
                    return publisher;
                }
                else
                {                    
                    return null;
                }
            }
        }
你们看看这代码跟上面的代码有上面区别!!
注意看下!!!
恩!对了!!就是下面的代码多一个using而且read并没关闭!!
由于using会自动释放资源!!所以我们就不用关闭read了!!
怎么样!!
using好用吧!!其实发生这个问题是因为我们的DBHelper设置的优点不正确!!等以后到了公司我就不在手写dbHerlper了!!直接调用微软为我们提供的sqlHelper!!