流数据到.csv不显示结果C#

时间:2018-12-13 19:10:41

标签: c# sql-server streamwriter data-stream

我正在尝试将数据从SQLServer DB流式传输到.CSV文件;但是,执行后,.csv仍为空白。第一个while循环用于测试目的,可以正确执行。我知道我在流媒体部分做错了。

任何帮助将不胜感激!

const foo = [{
    bar: {
      order: "hello" 
    } 
  },{
    bar: {
      order: "something" 
    } 
  },{
    bar: {
      order: "else" 
    } 
  },{
    bar: {
      order: "else" 
    } 
  },{
    bar: {
      order: "anotherelse" 
    } 
  }];

const order = { "something": 1, "hello": 3, "else": 2, "missing": 4, "anotherelse": 2 };

const orderWith = (array, order, desc) => 
    Object.keys(order)
          .sort((a, b) => desc ? order[b] - order[a] : order[a] - order[b])
          .reduce((acc, val) => 
              acc.concat(array.filter(({ bar: { order }}) => val === order))
          , []);

console.log('Descending order');
console.log(orderWith(foo, order));

console.log('Ascending order')
console.log(orderWith(foo, order, true));

编辑 我已经编辑了原始代码,以不包含每个建议的第一个while循环。我现在遇到以下错误:

  

System.ObjectDisposedException:'无法写入已关闭的TextWriter。'

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("Server=*******;Database=*********;Integrated Security=true");
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT p.cFName, p.cLName, s.cSugTitle, s.cEcrno, g.cName, h.mNotes, u.UserID, u.NetworkID FROM people p INNER JOIN suggest s ON p.cidPeople = s.cidPeople_WhoEntered INNER JOIN Grp g on s.cidGrp = g.cidGrp INNER JOIN history h ON h.cidSuggest = s.cidSuggest INNER JOIN users u ON u.cidPeople = p.cidPeople", conn);
            SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.IsDBNull(5) ? null : reader.GetString(5), reader.GetString(6), reader.IsDBNull(7) ? null : reader.GetString(7));
        }


        String path = @"C:\Users\AEEVANS\Desktop\example.csv";

        using (StreamWriter sr = File.AppendText(path))
        {
            while (reader.Read())
            {
                sr.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.IsDBNull(5) ? null : reader.GetString(5), reader.GetString(6), reader.IsDBNull(7) ? null : reader.GetString(7));
                sr.Close();
            }  
        }
        Console.ReadKey();

        reader.Close();
        conn.Close();

        if (Debugger.IsAttached)
        {
            Console.ReadLine();
        }
    }
  }
}

1 个答案:

答案 0 :(得分:3)

while (reader.Read())是通过数据读取器的单程旅行。如果您再执行一次while (reader.Read()),则由于指针已经位于阅读器内容的末尾,您将一无所获。

看起来您应该能够将两个while (reader.Read())块合并为一个。