Results 1 to 8 of 8

Thread: [RESOLVED] converting from text file to DB file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    Resolved [RESOLVED] converting from text file to DB file

    So i'm having a hard time converting a working version of my project that received all of its data from a text file, to now receive data from a database. Both have all the same information, but I can't figure it out.

    Heres the working version from the text file:
    Code:
    Private Sub LoadTranList()
            Dim tranFile As System.IO.StreamReader 'file of transaction data
            Dim tranFileName As String = "fatrans.txt"
            Dim nextTran As transaction
            Dim holdrecord As String
            Dim tid As String 'client id from file record
            Dim tcode As String 'stock code from file record
            Dim tdate As Date 'tran date from file record
            Dim ttype As String 'buy or sell from file record
            Dim tshares As Integer 'number of shares
            Dim tprice As Double 'share price
    
            'check to see if transaction file exists
            If Not System.IO.File.Exists(tranFileName) Then
                Throw New Exception("cannot find transaction file")
                Exit Sub
            End If
    
            'open the transaction file to start at first record
            tranFile = New System.IO.StreamReader(tranFileName)
    
            'read thru tran file, add client trans to list
            Do While tranFile.Peek <> -1
                holdrecord = tranFile.ReadLine() 'get next record
                'break record into fields
                tid = holdrecord.Substring(0, 3)
                tcode = holdrecord.Substring(4, 5).Trim
                tdate = CDate(holdrecord.Substring(10, 10))
                ttype = holdrecord.Substring(21, 1)
                tshares = CInt(holdrecord.Substring(23, 5))
                tprice = CDbl(holdrecord.Substring(29, 7))
    
                If CInt(tid) = m_ID Then 'create client tran, add to list
                    nextTran = New transaction(m_ID, tcode, tdate, ttype, tshares, tprice)
                    m_tranlist.Add(nextTran)
                End If
            Loop
        End Sub
    Now I can fill/populate a table with all of the information using:
    Code:
    Dim selectstring As String
            Dim connectionstring As String
            Dim transadapter As OleDb.OleDbDataAdapter
            Dim transtable As DataTable
    
            connectionstring = "Provider = Microsoft.JET.OLEDB.4.0;" & "Data Source = barestern2003.mdb"
    
            'create select statement
            selectstring = "select * from transact"
    
            'create adapter
            transadapter = New OleDb.OleDbDataAdapter(selectstring, connectionstring)
    
            'create the stock data table
            transtable = New DataTable
    
            transadapter.Fill(transtable) 'Fill the data table
    So I must be having trouble with the Do Loop, because I don't know how to read each individual line, like a text file. I know how to change all of the .Substring's in the text file to the .Item's from the DB, so that's not a problem Any suggestion? Thanks for any help!
    Last edited by ecotec8286; May 12th, 2011 at 11:30 PM. Reason: missing information

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: converting from text file to DB file

    Once you've got a DataTable you can loop through its Rows collection and get the data from the DataRows. Follow the Database FAQ link in my signature for more information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    Re: converting from text file to DB file

    Code:
    For counter = 0 To Table.Rows.Count - 1
                tid = holdrecord.Substring(0, 3)
                tcode = holdrecord.Substring(4, 5).Trim
                tdate = CDate(holdrecord.Substring(10, 10))
                ttype = holdrecord.Substring(21, 1)
                tshares = CInt(holdrecord.Substring(23, 5))
                tprice = CDbl(holdrecord.Substring(29, 7))
    
                If CInt(tid) = m_ID Then 'create client tran, add to list
                    nextTran = New transaction(m_ID, tcode, tdate, ttype, tshares, tprice)
                    m_tranlist.Add(nextTran)
                End If
    Next
    Would this be correct then?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: converting from text file to DB file

    Did it work when you tried it?
    Quote Originally Posted by jmcilhinney
    Follow the Database FAQ link in my signature for more information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    Re: converting from text file to DB file

    nope

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: converting from text file to DB file

    Then it's not correct and you should do as I've advised twice already.
    Follow the Database FAQ link in my signature for more information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    23

    Re: converting from text file to DB file

    sorry, dumb mistake by me, i figured it out! thanks for your help.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] converting from text file to DB file

    I had a look at the thread that I know has the answer you need and I will accept that that answer may not be immediately obvious, although it is there for those who are prepared to make the effort. With that in mind, here is an example of how you loop through a collection that is specifically relevant to this topic:
    vb.net Code:
    1. For Each row As DataRow In myDataTable
    2.     'Use row here
    3. Next
    That gives you access to the current row inside the loop. You can then get the field values by name from that row, an example of which you can you can find by doing as I have suggested.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width