?
Solved

Populate Dataset from a text file located in a ftp server

Posted on 2004-11-25
9
Medium Priority
?
350 Views
Last Modified: 2011-09-20
Hi experts,

I have an application that fills a dataset out of a text file. The text file is first downloaded from a ftp server.

I want to simplifly the application.

I can handle the ftp download easily on vb.net, but I want to know if its possible to allocate a dataset and dump the content of the file into that dataset.

example:

What I currently do is similar to:

get "myftpfile" "mylocalfile"

Once I have the "mylocalfile", I fill a dataset with the tab-delimited information from it.

Instead of phisically having the tab-delimited "mylocalfile" on my pc, I want to directly dump its contents into a dataset to eventually handle the datatable from there like:

get "myftpfile" "mydataset"  

Thanks

0
Comment
Question by:aarons34
  • 4
  • 3
  • 2
9 Comments
 
LVL 4

Expert Comment

by:w3tim
ID: 12680329
If the data is uniform i.e.
where "," = delimiter
data1.0,data2.0,data3.0,data4.0
data1.1,data2.1,data3.1,data4.1
data1.2,data2.2,data3.2,data4.2
etc.

Simply read through the text file, line by line.
On each line split the string into an array by the delimiters then add each item in the array into the relevent column in the dataset.

For the following example, i used the above example data using a "," as the delimiter!

Example:
    Public NewDataSet As DataSet
    Private Sub MyBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NewDataSet = New DataSet("DataSetName")
        NewDataSet.Tables.Add(New DataTable("DataToSave"))
        NewDataSet.Tables("DataToSave").Columns.Add(New DataColumn("Field1", GetType(String)))
        NewDataSet.Tables("DataToSave").Columns.Add(New DataColumn("Field2", GetType(String)))
        NewDataSet.Tables("DataToSave").Columns.Add(New DataColumn("Field3", GetType(String)))
        NewDataSet.Tables("DataToSave").Columns.Add(New DataColumn("Field4", GetType(String)))

        ReadTxtToXmlFile()
    End Sub
    Public Sub ReadTxtToXmlFile()
        Dim ReadTextFile As New System.IO.StreamReader(Application.StartupPath & "\testFile.txt")
        Do Until ReadTextFile.Peek = -1
            Dim LineArr As String() = Split(ReadTextFile.ReadLine, ",", 4)
            Dim NewDataRow As DataRow = NewDataSet.Tables("DataToSave").NewRow
            NewDataRow("Field1") = LineArr(0)
            NewDataRow("Field2") = LineArr(1)
            NewDataRow("Field3") = LineArr(2)
            NewDataRow("Field4") = LineArr(3)
            NewDataSet.Tables("DataToSave").Rows.Add(NewDataRow)
        Loop
        NewDataSet.WriteXml("XMLDataFile.xml", XmlWriteMode.WriteSchema)
    End Sub


Hope this helps, let me know if this isn't what you wanted!

Tim
0
 
LVL 1

Author Comment

by:aarons34
ID: 12682257
hi Tim,

Thanks for your help. However, the transformation of the text file doesn't work quite the same as you suggest when you read in a ftp server.

In your example, I would have to download the file to the local machine through FTP protocol anyways and that is what I want to avoid.

When opening the socket of the ftp, the stream of bytes transfer end up in a local file. I want that stream of bytes end up in a dataset.

I'm raising the points to 500.

Thanks
0
 
LVL 4

Expert Comment

by:w3tim
ID: 12682359
OK, so I assume my code is what you wanted appart from it requiring you to download the file first!?

That being the case, can you post the code you are using to currently download the files off your FTP server and I will try to modify it to write the data to a stream rather than a file.  Then the code I gave you above will still apply but rather than the streamreader reading a file it will read the downloaded stream directly!

Thanks,

Tim
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:aarons34
ID: 12682394
It doesn't apply because that is not what I need. As I stated in my question, I already do that. I have no problems reading a local file file and parsing it into a dataset and I don't have problem downloading the file because I use a third party ftp class that uses sockets.

Therefore, you are repeating what I already know. I want to mix both

Thanks

0
 
LVL 6

Expert Comment

by:platinumbay
ID: 12683372
What Tim suggested cuts of the local file, just read the FTP buffer into the dataset from the FTP stream.  Re-read it again.  There is no direct coversion between a data file and a dataset, without using XML and a schema.

Maybe you should post some code regarding what you are trying to do.
0
 
LVL 1

Author Comment

by:aarons34
ID: 12683844

Hi platinum,

The thing is that when you read from the local file, you can tell what is a tab, and what is data, but I don;t know exactly how can I do that (define the schema) for the FTP stream

Any ideas?
0
 
LVL 6

Accepted Solution

by:
platinumbay earned 2000 total points
ID: 12684066
How to Access a File Transfer Protocol Site by Using Visual Basic .NET

http://www.developer.com/net/vb/article.php/3424121
http://support.microsoft.com/kb/832679/EN-US/
0
 
LVL 4

Expert Comment

by:w3tim
ID: 12752796
Sorry for the long reply, I've been ill for the last week.  Something really nasty going around, everyone I know seems to be getting it!

Anyway, Platinumbay is right, the easiest way to do this is to hash up the standard code that Microsoft issue (there are some refined/improved versions around) for FTP.

I believe there is a DownloadFile sub in the code somewhere, i seem to remember using it.  It probably uses a filestream to output to file.  All you need to do is dump the data to the memory rather than a file and then read the stream into the code I gave before.
0
 
LVL 1

Author Comment

by:aarons34
ID: 12878832
Thank you all,

by modifying the way I transfer the file from the buffer to the local machine, I was able to create the dataset that I needed and load the file in memory to then process it.

Thanks again!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question