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)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>
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.