posts - 69, comments - 46, trackbacks - 0, articles - 1
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

在数据库操作里,不判断DBNLL的后果

Posted on 2006-10-17 10:39 God Blue Shadow 阅读(46) 评论(0) 编辑 收藏

try

{

 int iState = Convert.ToInt32( tbOUser.Rows[i][j].ToString() );

}

catch

{

item.SubItems.Add( "未知状态" );

}

tbOUser的类型为DataTable,如果tbOUser.Rows[i][j]是DBNull的话,系统会消耗大量内存,直到这个函数处理完了以后,系统才会起来,

但如果在前面加个判断的话

if ( DBNull.Value != tbOUser.Rows[i][j] )
      {
       try
       {
        int iState = Convert.ToInt32( tbOUser.Rows[i][j].ToString() );
       catch
       {
        item.SubItems.Add( "未知状态" );
       }
      }
      else
      {
       item.SubItems.Add( "未知状态" );
   }

}

系统立马就起来了,Convert.ToInt32和int.Parse在这里是一样的效果