change a file database to a dataset

I've got this code i found which opens a file and loads the records into a records set. thats greate but it does not really do what i need as i need to really read from a dataset as i need to update the data in the dataset in realtime and use the dataset for calculations.

what i thought i would do is just change the sub values as follows

private sub opendatabase( byref data as dataset, byref dt as datatable)

and then use these but i'm pretty new to all this and did not get far.

regards


Allan
Public Sub OpenDatabase(ByVal FileName As String, ByVal Delimiter As String)
 
        'Requires 6 fields: Date, Open, High, Low, Close, Volume
        'using any delimiter.
 
        Dim nFile As Integer
        Dim Record As String
        Dim Found As Short
        Dim Index As Short
 
        On Error GoTo ErrHndl
 
        m_Recordset = New RecordSet
        m_Date = New Field
        m_Open = New Field
        m_High = New Field
        m_Low = New Field
        m_Close = New Field
        m_Volume = New Field
 
        'Get record count
        nFile = FreeFile()
        FileOpen(nFile, FileName, OpenMode.Input, , OpenShare.LockRead) 'Open file
        Do While Not EOF(nFile)
            Record = LineInput(nFile)
            Index = Index + 1
        Loop
        m_RecordCount = Index
        FileClose(nFile)
        Index = 0
 
        'Initialize Recordsets
        m_Date.initialize(m_RecordCount, "Date")
        m_Open.initialize(m_RecordCount, "Open")
        m_High.initialize(m_RecordCount, "High")
        m_Low.initialize(m_RecordCount, "Low")
        m_Close.initialize(m_RecordCount, "Close")
        m_Volume.initialize(m_RecordCount, "Volume")
 
        nFile = FreeFile()
        FileOpen(nFile, FileName, OpenMode.Input, , OpenShare.LockRead) 'Open file
 
        Do While Not EOF(nFile)
 
            Index = Index + 1
            Record = LineInput(nFile) 'Grab one line from file
 
            'Date
            Found = InStr(Record, Delimiter)
            If Found <> 0 Then
                m_Date.setStrValue(Index, Mid(Record, 1, Found - 1))
                Record = Mid(Record, Found + 1)
            End If
 
            'Open
            Found = InStr(Record, Delimiter)
            If Found <> 0 Then
                m_Open.setValue(Index, CDbl(Mid(Record, 1, Found - 1)))
                Record = Mid(Record, Found + 1)
            End If
 
            'High
            Found = InStr(Record, Delimiter)
            If Found <> 0 Then
                m_High.setValue(Index, CDbl(Mid(Record, 1, Found - 1)))
                Record = Mid(Record, Found + 1)
            End If
 
            'Low
            Found = InStr(Record, Delimiter)
            If Found <> 0 Then
                m_Low.setValue(Index, CDbl(Mid(Record, 1, Found - 1)))
                Record = Mid(Record, Found + 1)
            End If
 
            'Close
            Found = InStr(Record, Delimiter)
            If Found <> 0 Then
                m_Close.setValue(Index, CDbl(Mid(Record, 1, Found - 1)))
                Record = Mid(Record, Found + 1)
            End If
 
            'Volume
            m_Volume.setValue(Index, CDbl(Record))
 
            System.Windows.Forms.Application.DoEvents()
        Loop
 
        MoveFirst()
 
        m_Recordset.addField(m_Date)
        m_Recordset.addField(m_Open)
        m_Recordset.addField(m_High)
        m_Recordset.addField(m_Low)
        m_Recordset.addField(m_Close)
        m_Recordset.addField(m_Volume)
 
        FileClose(nFile) 'Close file
 
        Exit Sub
ErrHndl:
        FileClose(nFile)
        Err.Raise(1012, "OpenDatabase", Err.Description)
 
    End Sub

Open in new window

allanhuttonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

allanhuttonAuthor Commented:
Okay i've work out how to do this with ADO but know that ado is not really the best approach to take in doing things quickly and is a little dated. what should i use oledb? but what do i need to do to change to oledb.

if you can help i would appreciated it.
   Public Sub OpenDatabase(ByVal Databasename As String, ByVal TableName As String)
 
        'Requires 6 fields: Date, Open, High, Low, Close, Volume
        'using any delimiter.
 
        Dim Record As String
        Dim Index As Integer
 
        On Error GoTo ErrHndl
 
        'Setup TA-SDK fields
        m_Recordset = New RecordSet
        m_Date = New Field
        m_Open = New Field
        m_High = New Field
        m_Low = New Field
        m_Close = New Field
        m_Volume = New Field
 
 
        'Connect to the database
        Dim cn As New sqlconnection
        Dim rs As New Sql
        Dim SQL As String = "SELECT * FROM " & TableName & " ORDER BY TickDate, HourInterval"
 
 
 
        cn.Open("Provider=sqloledb;Uid=;Pwd=;Server=127.0.0.1;Database=")
 
 
        rs.Open(SQL, cn, 1, 1)
 
 
        'Get record count
        m_RecordCount = rs.RecordCount
        Index = 0
 
        'Initialize Recordsets
        m_Date.initialize(m_RecordCount, "Date")
        m_Open.initialize(m_RecordCount, "Open")
        m_High.initialize(m_RecordCount, "High")
        m_Low.initialize(m_RecordCount, "Low")
        m_Close.initialize(m_RecordCount, "Close")
        m_Volume.initialize(m_RecordCount, "Volume")
 
 
        Do While Not rs.EOF
 
            Index = Index + 1
 
            'Date
            m_Date.setStrValue(Index, rs.Fields("TickDate").Value & " " & rs.Fields("HourInterval").Value)
 
            'Open
            m_Open.setValue(Index, rs.Fields("Open").Value)
 
            'High
            m_High.setValue(Index, rs.Fields("High").Value)
 
            'Low
            m_Low.setValue(Index, rs.Fields("Low").Value)
 
            'Close
            m_Close.setValue(Index, rs.Fields("Close").Value)
 
            'Volume
            'm_Volume.setValue Index, rs.Fields("Volume").Value
 
            rs.MoveNext()
 
        Loop
 
 
        MoveFirst()
 
        m_Recordset.addField(m_Date)
        m_Recordset.addField(m_Open)
        m_Recordset.addField(m_High)
        m_Recordset.addField(m_Low)
        m_Recordset.addField(m_Close)
        m_Recordset.addField(m_Volume)
 
        GoTo CleanUp
 
        Exit Sub
ErrHndl:
        Err.Raise(1105, "Database.OpenDatabase", "An unexpected error has occured")
CleanUp:
        rs.Close()
        cn.Close()
        rs = Nothing
        cn = Nothing
 
 
    End Sub

Open in new window

0
Priest04Commented:
ADO is definetly not the right way. Using DataTable is quite easy, but I am not sure whay exactly are you doing here. In the 1st example, you open a file, in a second example, you dont. Can you be more specific about what exactly are you after?

Goran
0
allanhuttonAuthor Commented:
the first example is the code used by the library i got to load data. they don't have an example that will read from a dataset. so i've tried to modify it to read from a database using adodb but i dont think using adodb was the way to go but i'm not to sure how to produce the same results with a dataset.

regards
Allan

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Priest04Commented:
I will post an example to give you the idea how it works.
Dim dt As New DataTable() ' create object DataTable
 
' create columns in DataTable that will hold data
dt.Columns.Add("date_field", GetType(DateTime)) ' Change to Appropriate field type
dt.Columns.Add("open_field", GetType(String)) ' Change to Appropriate field type
dt.Columns.Add("high_field", GetType(String)) ' Change to Appropriate field type
dt.Columns.Add("low_field", GetType(String)) ' Change to Appropriate field type
dt.Columns.Add("close_field", GetType(String)) ' Change to Appropriate field type
dt.Columns.Add("volume_field", GetType(Double)) ' Change to Appropriate field type
 
Dim dr As DataRow
 
Do While Not EOF(nFile)
    dr = dt.Rows.NeWRow()
    dr("date_field) = date_value ' replace date_value with value read from file
    dr("open_field) = date_value ' replace open_value with value read from file
    dr("high_field) = date_value ' replace high_value with value read from file
    dr("low_field) = date_value ' replace low_value with value read from file
    dr("close_field) = date_value ' replace close_value with value read from file
    dr("volume_field) = date_value ' replace volume_value with value read from file
 
    dt.Rows.Add(dr)
Loop
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Priest04Commented:
Sorry, I made a typing mistake, all code that assigns a value to DataRow field is missing double quote ". Example

dr("date_field") = date_value ' replace date_value with value read from file
0
allanhuttonAuthor Commented:
instead of dr=dt.rows.newrow() generates a syntax error  is it just dr=dt.newrow()

finally how do i iterate through each record in the datatable

regards
allan
0
Priest04Commented:
Yes, you are correct, I was typing from head. :) To iterate through each row


For Each dr As DataRow In dt.Rows
    MessageBox.Show(dr("date_field").ToString())
Next

Open in new window

0
allanhuttonAuthor Commented:
thats for helping me work through the problem and asking further questions to get to this problem resolve.

cheers
Allan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.