C
Chris Lee
I am using the following code below to update a simple Access database
with historical stock prices:
-------------------code
snippet----------------------------------------
'Open connection to update data
Dim myConn As New OleDbConnection(ConnectString)
myConn.Open()
'Create the SQL queries
Dim SQLSecurity As String = "SELECT * FROM [Security]"
Dim SQLHistory As String = "SELECT * FROM [History]"
'Create new DataAdapter objects
Dim SecDataAdapter As New OleDbDataAdapter(SQLSecurity, myConn)
Dim HistDataAdapter As New OleDbDataAdapter(SQLHistory, myConn)
'Create new datasets
Dim SecDataSet As New DataSet()
Dim HistDataSet As New DataSet()
'Fill datasets to store existing data
SecDataAdapter.Fill(SecDataSet, "Security")
HistDataAdapter.Fill(HistDataSet, "History")
'Create command builders which is necessary to update the database
Dim SecCommandBuilder As OleDbCommandBuilder = New
OleDbCommandBuilder _(SecDataAdapter)
Dim HistCommandBuilder As OleDbCommandBuilder = New
OleDbCommandBuilder _(HistDataAdapter)
For i = 0 To SecDataSet.Tables("Security").Rows.Count - 1
'Get the current datarow from DataTable
Dim row As DataRow = SecDataSet.Tables("Security").Rows(i)
'Construct a valid security ticker
Dim strSecurity As String = row("Ticker").ToString
'Calling a function that will return historical data for a
security
Dim oData As Object = GetHistoricalData(strSecurity)
'Add historical data to DataSet
For j = 0 To oData.GetUpperBound(0)
Dim NewRow As DataRow = HistDataSet.Tables("History").NewRow
NewRow("Ticker") = row("Ticker").ToString
NewRow("Date") = Convert.ToDateTime(oData(j, 0))
NewRow("Price") = Convert.ToSingle(oData(j, 1))
HistDataSet.Tables("History").Rows.Add(NewRow)
Next
HistDataSet.Tables("History").AcceptChanges()
Dim count As Integer = oData.GetUpperBound(0)
row("LastUpdate") = Convert.ToDateTime(oData(count, 0))
row("DataCount") = Convert.ToInt32(count)
SecDataSet.Tables("Security").AcceptChanges()
'Update database with the updated information
SecDataAdapter.Update(SecDataSet, "Security")
HistDataAdapter.Update(HistDataSet, "History")
Next
'Display results
Dim StatusForm As frmStatus
StatusForm = New frmStatus()
StatusForm.MdiParent = Me
StatusForm.DataGrid1.DataSource = SecDataSet.DefaultViewManager
StatusForm.Show()
'Close connection
myConn.Close()
-------------------------code
ends------------------------------------------
The code failed to update the Access database with the data. I did not
encounter any run-time error while executing the code. What surprises
me is that the StatusForm->DataGrid1 shows that the SetDataSet
contained the historical data.
Could someone please shed some light how I may rectify the problem?
Thanks
Chris
with historical stock prices:
-------------------code
snippet----------------------------------------
'Open connection to update data
Dim myConn As New OleDbConnection(ConnectString)
myConn.Open()
'Create the SQL queries
Dim SQLSecurity As String = "SELECT * FROM [Security]"
Dim SQLHistory As String = "SELECT * FROM [History]"
'Create new DataAdapter objects
Dim SecDataAdapter As New OleDbDataAdapter(SQLSecurity, myConn)
Dim HistDataAdapter As New OleDbDataAdapter(SQLHistory, myConn)
'Create new datasets
Dim SecDataSet As New DataSet()
Dim HistDataSet As New DataSet()
'Fill datasets to store existing data
SecDataAdapter.Fill(SecDataSet, "Security")
HistDataAdapter.Fill(HistDataSet, "History")
'Create command builders which is necessary to update the database
Dim SecCommandBuilder As OleDbCommandBuilder = New
OleDbCommandBuilder _(SecDataAdapter)
Dim HistCommandBuilder As OleDbCommandBuilder = New
OleDbCommandBuilder _(HistDataAdapter)
For i = 0 To SecDataSet.Tables("Security").Rows.Count - 1
'Get the current datarow from DataTable
Dim row As DataRow = SecDataSet.Tables("Security").Rows(i)
'Construct a valid security ticker
Dim strSecurity As String = row("Ticker").ToString
'Calling a function that will return historical data for a
security
Dim oData As Object = GetHistoricalData(strSecurity)
'Add historical data to DataSet
For j = 0 To oData.GetUpperBound(0)
Dim NewRow As DataRow = HistDataSet.Tables("History").NewRow
NewRow("Ticker") = row("Ticker").ToString
NewRow("Date") = Convert.ToDateTime(oData(j, 0))
NewRow("Price") = Convert.ToSingle(oData(j, 1))
HistDataSet.Tables("History").Rows.Add(NewRow)
Next
HistDataSet.Tables("History").AcceptChanges()
Dim count As Integer = oData.GetUpperBound(0)
row("LastUpdate") = Convert.ToDateTime(oData(count, 0))
row("DataCount") = Convert.ToInt32(count)
SecDataSet.Tables("Security").AcceptChanges()
'Update database with the updated information
SecDataAdapter.Update(SecDataSet, "Security")
HistDataAdapter.Update(HistDataSet, "History")
Next
'Display results
Dim StatusForm As frmStatus
StatusForm = New frmStatus()
StatusForm.MdiParent = Me
StatusForm.DataGrid1.DataSource = SecDataSet.DefaultViewManager
StatusForm.Show()
'Close connection
myConn.Close()
-------------------------code
ends------------------------------------------
The code failed to update the Access database with the data. I did not
encounter any run-time error while executing the code. What surprises
me is that the StatusForm->DataGrid1 shows that the SetDataSet
contained the historical data.
Could someone please shed some light how I may rectify the problem?
Thanks
Chris