日期:2014-05-18  浏览次数:21085 次

C#代码中的Throw 为什么报错 ,注释提示说没有返回值,该怎么修改呢:
请问各位大侠,C#代码中的Throw 为什么报错 ,注释提示说没有返回值,该怎么修改呢:

1、业务实体:
  public class Article
  {
  private int _articleId;  

  public int ArticleId
  {
  get { return _articleId; }
  set { _articleId = value; }
  }  

}
2、调用通用的SQLHelper ,数据访问如下:
 public static Article GetArticleByArticleId(int id)
  {
  string sql = "select * from article where articleId=@articleId";
  try
  {
  using (SqlDataReader reader = SqlHelper.GetReader(sql, new SqlParameter("@articleId", id)))
  {
  if (reader.Read())
  { return FillRecord(reader); }
  else
  { return null; }
  }
  }
  catch (Exception e)
  {
  Console.WriteLine(e.Message); throw e;
  }
  }

3、业务逻辑
public static class ArticleManager
  {
  public static Article GetArticlesByArticleId(int id)
  {
  return ArticleService.GetArticleByArticleId(id);
  }

  }

[b]程序运行显示 数据访问错误显示:
throw e处; 对象不能从 DBNull 转换为其他类型。请各位指示
[color=#0000FF][/color]

------解决方案--------------------
探讨
对象不能从 DBNull 转换为其他类型。请各位指示

------解决方案--------------------

 if(reader["articleId"]!=null)
{
a.ArticleId = Convert.ToInt32(reader["articleId"]);
}

所有全加if 肯定有空的
------解决方案--------------------
探讨
/// <summary>
/// 实体对象填充业务对象公共方法
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
private static Article FillRecord(SqlDataReader reader)
{
Article a = new……

------解决方案--------------------
探讨
引用:
/// <summary>
/// 实体对象填充业务对象公共方法
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
private static Article FillRecord(SqlDataReader reader)
{
Art……

------解决方案--------------------
1、
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
这里throw e 要改成throw
不然的话调试的时候会人为错误是从 throw e;这行代码中产生的。
2、
你说提示无将DBNull 转换成其他类型,说明你查询的数据中有字段是为null的。这个时候要人为的进行转换。
因为数据库中存储null值和程序中存储null值是有区别的
------解决方案--------------------
C# code

if(reader["articleId"]!=DBNull.value)
{
   a.ArticleId = Convert.ToInt32(reader["articleId"]);
}