SqlDataReader reader = ....DateTime d = DateTime.MinValue; //DateTime类型不能赋为null,所以赋初值为.Net中DateTime类型的最小值if( reader[0] == DBNull.Value )
   d = (DateTime)reader[0];

解决方案 »

  1.   

    我从数据库中获取得的字段是container_time_in 如何判断它是不是DBNull.Value.其中container_time_in 十DateTime类型.
      

  2.   

    是想在Sql中呢,还是在程序中呢
    在前者,可以这样:
    string strQuery="select * from yourtable where yourfield is not null";
    如果是后者,用
    try……catch进行捕获。
      

  3.   

    try
    {
    ContainerMFService cmfService=new ContainerMFService();
    ContainerMFDataSet cmfDs=new ContainerMFDataSet();
    cmfDs=(ContainerMFDataSet)SerializeDataSet.Deserialize(cmfService.getContainerByContainerId(this.textBox3.Text.Trim().ToString(),"1556"),"ContainerMF"); ContainerMFDataSet.ContainerMFRow r=(ContainerMFDataSet.ContainerMFRow)cmfDs.ContainerMF.Rows[0];
    if(r.container_empty_time==DBNull.Value)
    {
    this.txtEmptyTime.Text=r.container_empty_time.ToString("yyyy-MM-dd");
    }
    if(r.container_out_time.Equals(DBNull.Value))
    {
    this.txtOutTime.Text=r.container_out_time.ToString("yyyy-MM-dd");
    }
    }
    catch(Exception eX)
    {
    MessageBox.show(EX.Message);
    }我这里有些特殊,在程序中进行判断,其他字段不为空,而这个字段有时候为空,有时候不为空,所以无法再server中判断.通过异常是扑或了,但是怎么处理呢?
    假如这样if(container_in_time!=DBNull.Value)
    {MessageBox.show("there is no container");}
    else
    {
    MessageBox.show("");
    }
      

  4.   

    如果嫌麻烦,那么直接try catch不就行了吗?
    try
    {
        this.txtEmptyTime.Text=r.container_empty_time.ToString("yyyy-MM-dd");
    }
    catch
    {
        MessageBox.show("there is no container"); 
    }