Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to export a sql datatable to notepad file without looping through each row.
I have a data table, containing almost 60 to 70 lacks of rows.
It is very time consuming to loop through every row and write it in text file.

Tools:
Framework 2.0
Language VB.Net

Please Advice...
Posted
Comments
Scubapro 20-Jan-12 7:07am    
Why do you say it's time consuming? I've done this is in one of my projects for writing (and reading) a datatable with more then 300 rows (and 13 columns) to a textfile. I think you'll be suprised just how fast this works!
Harshad-HBK 23-Jan-12 3:55am    
If so then, please explain in bit detail.
For me suggestions are always welcome.
Scubapro 23-Jan-12 4:12am    
It's like E.F Nijboer says; use a For/Next loop for your rows, use a streamreader, open your file, use WriteLine to write your rows, close the file.
Harshad-HBK 27-Jan-12 4:04am    
Thnks for guidance.. Problem solved!

1 solution

If you use the example in the following link I believe you when you say it is very slow.
http://www.dotnetspider.com/resources/28359-Export-DataGridView-into-csv-file.aspx[^]

The reason for the code to be slow is the fact that it concatenates strings over and over again. (eg. strExport += dc.Name + " ";) Because strings are immutable in .NET it will create a completely new string every time you append something to the string. A new string is initialized and will copy the contents you got so far into it with the new string added to it. With the code in the above link there will be a lot of data moving around in memory.

To make the code faster you can use the StringBuilder (examples: http://www.dotnetperls.com/stringbuilder-vbnet[^]) or simply open the StreamReader and write out everything immediately instead of first storing it all in memory. You can use Write for each row value and a WriteLine when done with a complete row. Your export will go extremely fast this way.

Good luck!
 
Share this answer
 
Comments
Harshad-HBK 27-Jan-12 4:06am    
Thanq E.F. Nijboer I got solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900