我的项目中有 SqlConnection,我想控制我的查询结果
public IEnumerable<Product> ListPrpductByCategory(int ID)
{
var dr = db.ExecuteReader(@"SELECT P.ID,P.CategoryID,P.Name,P.SupplierID,p.UnitPrice,p.UnitsInStock,pp.PicturePath
FROM Products P
LEFT JOIN ProductPhoto PP ON p.ID=PP.ProductID
WHERE P.CategoryID=@ID",
Values: new object[] { ID });
while (dr.Read())
{
yield return new Product()
{
ID = dr.GetInt32(0),
CategoryID = dr.GetInt32(1),
SupplierID = dr.GetInt32(3),
Name = dr.GetString(2),
UnitPrice = dr.GetDecimal(4),
UnitInstock = dr.GetInt16(5),
PicturePath = dr.GetString(6)
};
}
dr.Close();
//...
}
如果没有图片,它会抛出我想控制dr.GetString(6)
的错误
if(dr.GetString(6)==null)
PicturePath="Noimage.jpg";
我怎样才能做到这一点?