public BookEntity GetBookByID(int id)
{
//BookEntity result = null;
using (SqlConnection conn = this.CreateConnection())
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select * from ebook_Book Where ID = @ID";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@ID"].Value = id; conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
BookEntity entity = new BookEntity(); //"BookEntity"方法没有采用0个参数的重载
entity.ID = (int)reader["ID"];
entity.CategoryID = (int)reader["CategoryID"];
entity.Title = (string)reader["Title"];
entity.Description = (string)reader["Description"];
entity.Price = (decimal)reader["Price"];
entity.ImageUrl = (string)reader["ImageUrl"];
entity.OrderBy = (int)reader["OrderBy"];
return entity;
}
} conn.Close();
}
}
return null;
}为什么会发生 "BookEntity"方法没有采用0个参数的重载
而我在BookEntity中加入以下这句问题就解决了,为什麽啊?
public BookEntity()
{
}
顺便问一下,有两本关于asp.net书《ASP.NET 2.0数据库入门经典(第4版)》(Wrox红皮书) 和《ASP.NET 2.0数据库入门经典(特别版)》有什么区别啊??
{
//BookEntity result = null;
using (SqlConnection conn = this.CreateConnection())
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select * from ebook_Book Where ID = @ID";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@ID"].Value = id; conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
BookEntity entity = new BookEntity(); //"BookEntity"方法没有采用0个参数的重载
entity.ID = (int)reader["ID"];
entity.CategoryID = (int)reader["CategoryID"];
entity.Title = (string)reader["Title"];
entity.Description = (string)reader["Description"];
entity.Price = (decimal)reader["Price"];
entity.ImageUrl = (string)reader["ImageUrl"];
entity.OrderBy = (int)reader["OrderBy"];
return entity;
}
} conn.Close();
}
}
return null;
}为什么会发生 "BookEntity"方法没有采用0个参数的重载
而我在BookEntity中加入以下这句问题就解决了,为什麽啊?
public BookEntity()
{
}
顺便问一下,有两本关于asp.net书《ASP.NET 2.0数据库入门经典(第4版)》(Wrox红皮书) 和《ASP.NET 2.0数据库入门经典(特别版)》有什么区别啊??
{
}
缺少此构造函数的情况下
当你的实体类被实例化的时候
对象首先初始化构造函数
BookEntity entity = new BookEntity();"new BookEntity();" 这一句是初始化你的类的构造函数但是现在又找不到该构造函数,所以抛出你那个异常
{}方法了?
public BookEntity(DataRow row)
{
ID = (int)row["ID"];
CategoryID = (int)row["CategoryID"];
Title = (string)row["Title"];
Description = (string)row["Description"];
Price = (decimal)row["Price"];
ImageUrl = (string)row["ImageUrl"];
OrderBy = (int)row["OrderBy"];
} public BookEntity(SqlDataReader row)
{
ID = (int)row["ID"];
CategoryID = (int)row["CategoryID"];
Title = (string)row["Title"];
Description = (string)row["Description"];
Price = (decimal)row["Price"];
ImageUrl = (string)row["ImageUrl"];
OrderBy = (int)row["OrderBy"];
}
public BookEntity()
{
}方法
而你写的
BookEntity entity = new BookEntity();
为不带参数的
public BookEntity()
{
} 方法