C# StreamWriter 在完成之前停止?

C# StreamWriter stops before finishing?

using System;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;

namespace PullData
{
    class Program
    {
        static void Main(string[] args)
        {
            OleDbConnection connLocal = new OleDbConnection("Provider=SAOLEDB.10;Data Source=demo;Persist Security Info=True;User ID=dba;PWD=sql;Location=1.2.3.4");
            OleDbCommand cmdLocal = new OleDbCommand("SELECT tran_num, provider_id, amount, tran_date, collections_go_to, impacts, type, 'TestClinic' AS Clinic FROM transactions WHERE tran_date > '2015-09-27'", connLocal);
            StreamWriter sqlWriter = new StreamWriter(@"C:\Users\Administrator\Desktop\Clinic.txt");


            try
            {
                connLocal.Open();
            }
            catch (Exception connerr) { Debug.WriteLine(connerr.Message); }

            OleDbDataReader readLocal = cmdLocal.ExecuteReader();

            while (readLocal.Read())
            {
                sqlWriter.WriteLine("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}", readLocal.GetValue(0).ToString(), readLocal.GetValue(1).ToString(), readLocal.GetValue(2).ToString(), readLocal.GetValue(3).ToString(), readLocal.GetValue(4).ToString(), readLocal.GetValue(5).ToString(), readLocal.GetValue(6).ToString(), readLocal.GetValue(7).ToString());
            }
            readLocal.Close();
            connLocal.Close();
        }
    }
}

大家好,

以上是我目前正在测试的将查询结果写入 txt 文件的代码。除了每次我尝试 运行 它之外,一切似乎都有效,它似乎在结束程序之前在 运行dom 点停止写入。查询 运行ning 有 3370 行以 tran_num 227239 结尾,但是我的 txt 文件的末尾在到达那里之前就结束了,并且在一行中间被截断,如下所示:

227233|999|-5.00|11/3/2015 12:00:00 AM|999|C|A|TestClinic
227234|AK|0.00|11/3/2015 12:00:0

我已经尝试 运行 这几次遇到同样的问题,我 运行 只查询了数据库(使用相同的应用程序)并且可以看到所有行.关于为什么会发生这种情况的任何想法?谢谢

您应该使用 using 语句来确保您的一次性对象在出现异常时也被关闭。此外,为确保在完成写入后所有内容都刷新到磁盘,请调用 StreamWriter Flush 方法

static void Main(string[] args)
{
    using(OleDbConnection connLocal = new OleDbConnection(...))         
    using(OleDbCommand cmdLocal = new OleDbCommand("SELECT tran_num, provider_id, amount, tran_date, collections_go_to, impacts, type, 'TestClinic' AS Clinic FROM transactions WHERE tran_date > '2015-09-27'", connLocal))
    using(StreamWriter sqlWriter = new StreamWriter(@"C:\Users\Administrator\Desktop\Clinic.txt"))

    {
        try
        {
            connLocal.Open();
            using(OleDbDataReader readLocal = cmdLocal.ExecuteReader())
            {
                 while (readLocal.Read())
                 {
                    sqlWriter.WriteLine("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}", 
                    readLocal.GetValue(0).ToString(), 
                    readLocal.GetValue(1).ToString(),
                    readLocal.GetValue(2).ToString(), 
                    readLocal.GetValue(3).ToString(), 
                    readLocal.GetValue(4).ToString(), 
                    readLocal.GetValue(5).ToString(), 
                    readLocal.GetValue(6).ToString(), 
                    readLocal.GetValue(7).ToString());
                }
            }
            sqlWriter.Flush();
       }
       catch (Exception connerr) { Debug.WriteLine(connerr.Message); }
   }
}

另请注意,我已将所有代码包含在 try/catch 中,而不仅仅是打开连接并删除了对 Close 的调用,因为它会在您退出 using 块时自动调用。