写文本文件非常快 [英] Writing text file very fast

查看:81
本文介绍了写文本文件非常快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用c#windows应用程序并在数据加载器循环时附加文本文件。考虑情况-----------------

在excel文件中有广播,因此数据正在更新。现在我想将数据保存在文本文件中。我知道由于文件读取会遗漏一些数据需要一些时间。这就是为什么我想知道有效的方法。

excel是> = 500行。

现在我已经为那个excel和datareader创建了一个select查询数据获得如下 -

private void readWriteData()

{

oledbcommand cmd = new oledbcommand(query);

oledbdatareader dr = cmd.executeReader();

while(dr.Read())

{



}

dr.close();

}



一遍又一遍地调用readWriteData() (在完成之后再次调用它)由后台工作人员完成。所以它被称为越快,我就可以捕获更多的数据变化(滴答)。



文本输出为---

1-50-100

2-30-200

3-70-50







1-30-100

2-25-300

3-71-49







1,2,3是符号代码,30,100 s是数据,这些是波动。

所以读完1到500之后,程序会再次从1开始读取。



1)我可以这样做这-----------

Hi,
I am using c# windows application and appending a text file while a datareader is looping. Consider the situations-----------------
In an excel file there is a broadcast,so data is being updated. Now I want to save the data in text file. I know some data will be missed due to file reading will take some time. Thats why I want to know efficient way.
The excel is of >=500 rows.
now I have created a select query to that excel and in datareader the data is fetched like-
private void readWriteData()
{
oledbcommand cmd=new oledbcommand(query);
oledbdatareader dr=cmd.executeReader();
while(dr.Read())
{

}
dr.close();
}

readWriteData() is being called over and over(after its finished then again its called) by background worker. So the more fast it will be called the more data changes (ticks) I can capture.

output in text is as---
1-50-100
2-30-200
3-70-50
.
.
.
1-30-100
2-25-300
3-71-49
.
.

1,2,3 is code of symbol and the 30,100 s are data and those are fluctuating.
So after reading and writing 1 to 500 then again program will start reading from 1.

1)I can do it like this-----------

<pre lang="C#">
while(dr.Read())
{
 using (FileStream fs = new FileStream(fileName,FileMode.Append, FileAccess.Write))
 using (StreamWriter sw = new StreamWriter(fs))
 {
    sw.WriteLine(dr[0].toString()+"-"+dr[1].toString());
 }
} 





2)或者像这样------------ -





2)Or like this-------------

<pre lang="C#">

string s="";
while(dr.Read())
{
 s+=dr[0].toString()+"-"+dr[1].toString()+Environment.NewLine;
} 
dr.close();
File.WriteAllText(path, s);
</pre>







请帮助我的方案中最好的东西。即使你可以建议我可以做的新事物。请记住,我的目标是将广播数据从大型Excel保存到文本文件。所以我可以捕获的滴答数据(数据上下)越多越好。




Please help what is best in my scenario. Even you can suggest new thing what I can do. Just mind it my target is saving a broadcast data from a large excel to text file. So the more ticks (data up and down) I can capture,the more it is fine.

推荐答案

试试吧:

Try just:
File.AppendAllText(filename, string.Format("{0}-{1}\n", dr[0], dr[1]));



但是......我不知道它会像你一样工作,因为Read命令会结束当它用完了行给你时 - 即使有更多人将很快到达源头。另外,我不确定使用阅读器是否会锁定Excel文件,导致更新无法写入 - 这可能就是您丢失数据的原因。
我可能使用DataAdapter和DataTable在单个操作中尽可能快地读取整个内容,然后在重复操作之前将其处理到文本文件中。


But...I don;t know that it will work as you have it, since the Read command will end when it runs out of rows to feed you - even if more will be arriving at the source soon. Additionally, I'm not sure that the use of a reader won't "lock" the Excel file preventing the updates from writing to it - which could be why you lose data.
I'd probably use a DataAdapter and a DataTable to read the whole thing as fast as possible in a single operation, then process that into your text file before repeating the operation.


这篇关于写文本文件非常快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆