C#StreamWriter在完成之前停止?
大家好 上面是我目前正在测试的代码,用于将查询结果写入txt文件。除了每次我尝试运行它时,它似乎都会在程序结束前随机停止编写之外,所有这些似乎都正常工作。查询运行有3370行以227239的结尾结束,但是我的txt文件的结尾在到达那里之前结束,并且在这样的行中间截断:C#StreamWriter在完成之前停止?,c#,sqlanywhere,C#,Sqlanywhere,大家好 上面是我目前正在测试的代码,用于将查询结果写入txt文件。除了每次我尝试运行它时,它似乎都会在程序结束前随机停止编写之外,所有这些似乎都正常工作。查询运行有3370行以227239的结尾结束,但是我的txt文件的结尾在到达那里之前结束,并且在这样的行中间截断: using System; using System.Data.OleDb; using System.Diagnostics; using System.IO; namespace PullData { class P
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();
}
}
}
我尝试过几次使用相同的问题来运行这个,我只对数据库运行了查询(使用相同的应用程序),并且可以看到所有的行。有没有想过为什么会发生这种情况?谢谢您应该使用using语句来确保您的一次性物品在出现异常情况时也已关闭。此外,为了确保在完成写入后将所有内容刷新到磁盘,请调用StreamWriter Flush方法
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
还请注意,我将所有代码都包含在try/catch中,而不仅仅是打开连接,并删除了对Close的调用,因为当您退出using块时,将自动调用它。在关闭流之前尝试刷新流。在退出程序之前,请尝试关闭流。@omaphyxiate我个人会编写一个方法,检查文件是否存在,或者写入文件。我将发布一个示例作为答案,更重要的是此代码
OleDbDataReader readLocal=cmdLocal.ExecuteReader()代码>需要在Try中执行{}同时将while循环移动到Try中,或者将OleDbDataReader readLocal
的声明移动到Try{}之外。我添加了sqlWriter.Flush()代码>进入我的while,并sqlWriter.Close()代码>在关闭阅读器之前,它似乎正在写出所有的结果。谢谢你的洞察力@史蒂夫关于使用
块的回答是最好的答案,我相信。每当您怀疑对象实现了IDisposable
时,请始终使用using
块。事实上,我在大多数情况下都尝试过它,只有当VisualStudio通知我该对象实际上没有实现这个接口时,我才会删除它。
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); }
}
}