SQL reader多个结果不返回所有行

时间:2016-06-23 04:50:10

标签: c# .net sql-server

我从查询中获取多个结果并将其写入.CSV文件。问题是查询返回1700行,但此代码仅向文件写入1300行。例如,我错过了行1301到1700.我想知道这段代码的问题是什么:

using (SqlDataReader reader = exportCmd.ExecuteReader())
using (StreamWriter writer = new StreamWriter(exportFilename))
{
    string Separator = ",";

    while (reader.HasRows)
    {
        while (reader.Read())
        {
            for (int columnCounter = 0; columnCounter < reader.FieldCount; columnCounter++)
            {
                if (columnCounter > 0)
                {
                    writer.Write(Separator);
                }

                writer.Write(reader.GetValue(columnCounter).ToString());
            }

            writer.WriteLine(string.Empty);
        }

        reader.NextResult();
    }

    writer.Dispose();
}

提前致谢

3 个答案:

答案 0 :(得分:0)

您可以使用SqlDataReader.IsDBNull检查列是否为空,并删除了Yosi Dahari提到的冗余数据。

using (SqlDataReader reader = exportCmd.ExecuteReader())
using (StreamWriter writer = new StreamWriter(exportFilename))
{
    string Separator = ",";                        
    while (reader.Read())
    {
        for (int columnCounter = 0; columnCounter < reader.FieldCount; columnCounter++)
        {
            if (columnCounter > 0)                              
                writer.Write(Separator);

            if (reader.IsDBNull(columnCounter))
                writer.Write(string.Empty);
            else 
                writer.Write(reader.GetValue(columnCounter).ToString());                                                                        
        }
        writer.WriteLine(string.Empty);
    }                                                
    writer.Dispose();
}

答案 1 :(得分:0)

这应该可以解决问题

根据您的代码,我删除了所有不必要的代码行。 正如其他人已经说过的那样,你正在跳过一些行reader.NextResult(); 此外,当您处于使用块

时,您不必处置读取器/写入器
using (SqlDataReader reader = exportCmd.ExecuteReader()) {
        using (StreamWriter writer = new StreamWriter(exportFilename))
        {
          string Separator = ",";
          while (reader.Read())
          {
            for (int columnCounter = 0; columnCounter < reader.FieldCount; columnCounter++)
            {
              if (columnCounter > 0)
              {
                writer.Write(Separator);
              }
              writer.WriteLine(reader.GetValue(columnCounter).ToString());
            }
          }
        }
      }

答案 2 :(得分:0)

你的一个结果不返回任何行,这就是为什么你缺少行,外部while(reader.HasRows)退出而不检查下一个结果。

 using (SqlDataReader reader = exportCmd.ExecuteReader())
    using (StreamWriter writer = new StreamWriter(exportFilename))
    {
        string Separator = ",";

        while (reader.HasRows)
        {
            while(reader.Read())
            {
                for (int columnCounter = 0; columnCounter < reader.FieldCount; columnCounter++)
                {
                    if (columnCounter > 0)
                    {
                        writer.Write(Separator);
                    }

                    writer.Write(reader.GetValue(columnCounter).ToString());
                }

                writer.WriteLine(string.Empty);
            }


            While(reader.NextResult() && !reader.hasrows)
            {
// might be a bette way to do this , problem is that one of your result has no rows, then it exits...
            }

        }

        writer.Dispose();
    }