SQL 语句如下:
        string line1SJFloat = "select Convert(varchar(50),Convert(decimal(8,2),Round(sum(passqty)*1.0/sum(qty)*100,2,0))) + '%' as Yield from ir where ir.line ='01' and ir.step=(select config.cdesc from config where config.code='01')";C#语句如下:        myconn.Open();
        SqlDataReader Line1SJRead = line1SJSelectFloat.ExecuteReader();
        TextBox7.Text = Line1SJRead.GetString(0).ToString();
        Line1SJRead.Read();
        myconn.Close();现在的问题是,在SELECT的结果不为空时正常,但当SELECT结果为空时,提示说"在没有任何数据时进行无效的读取尝试".请问这个问题怎么解决,怎么可以将空值也显示出来,谢谢!

解决方案 »

  1.   

    myconn.Open();
    SqlDataReader Line1SJRead = line1SJSelectFloat.ExecuteReader();if(Line1SJRead.Read())
        TextBox7.Text = Line1SJRead.GetString(0).ToString();Line1SJRead.Close();
    myconn.Close();
      

  2.   

    sql语句可以加一个where  条件is not  null.不让他查为空的。
    或者try
    {
    }
    catch(()
    {}
      

  3.   

    读取一个值的情况下最好用ExecuteScalar方法:
    SqlCommand cmd = new SqlCommand("line1SJFloat", myconn);
    object obj = cmd.ExecuteScalar();
    if (obj != null)
    {
        TextBox7.Text = obj.TOString();
    }
    myconn.Close();