C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » VB.NET Syntax »

Export Dataset to CSV File using VB.Net


Posted Date: 28 Jun 2009    Resource Type: Code Snippets    Category: VB.NET Syntax
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following VB.Net Code Snippet is used to export Dataset to csv File.

All Data tables of a data set will be exported to CSV File.




Private Sub ExportDatasetToCsv(ByVal MyDataSet As DataSet)

'Declaration of Variables
Dim dt As DataTable
Dim dr As DataRow

Dim myString As String
Dim bFirstRecord As Boolean = True

Dim myWriter As New System.IO.StreamWriter("C:\MyTestCSV.csv")


myString = ""

Try

For Each dt In MyDataSet.Tables

For Each dr As DataRow In dt

bFirstRecord = True

For Each field As Object In dr.ItemArray

If Not bFirstRecord Then

myString.AppendText(",")


End If

myString.AppendText(field.ToString)

bFirstRecord = False

Next

'New Line to differentiate next row
myString.AppendText(Environment.NewLine)

Next

Next

Catch ex As Exception

MsgBox (ex.Message)


End Try


End Sub

'Write the String to the Csv File

myWriter.WriteLine(myString)

'Clean up
myWriter.Close()


End Sub




USAGE

ExportDatasetToCsv (ds)


Where ds is the dataset.


It will export all data tables of data set to csv File C:\MyTestCSV.csv




For more details, visit http://vijirajkumar.blogspot.com/2009/06/export-dataset-to-csv-file-using-vbnet.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Export Dataset to CSV File using VB.Net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: To Read a Field in table from dataset Session using VB.Net
Previous Resource: Creating Menu Items Programatically in VB.Net
Return to Discussion Resource Index
Post New Resource
Category: VB.NET Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use