views:

17

answers:

0

I'm trying to migrate from an Access db to a MySql and don't have much experience with the latter.

I wrote some sample code to write data that works one way, but not another and I'm not seeing the difference. Basically, the way that WORKS connects to the MySql database, extracts a dataset, creates a new row of data, adds it to the dataset, and then writes to the dataset and the changes are successfully transferred to the db.

The way that DOESN'T work fills a dataset from the original database, BUT there is more than one table which is the only difference I can see, but don't think it should affect what I'm trying to do. Then, I create a datarow and add it to the dataset and try to write the dataset, but the changes won't reflect to the db. I will note that the changes DO reflect to the dataset both ways, but only not to the db the second way.

I've copied all my code below. If I run "ShowAnnouncement", it works. If I run "Main", it won't work. What am I doing wrong?

Imports System.Data.Odbc

Module mMain Public dbcRuntest As OdbcConnection ' NEW, STANDARDIZED MySql db Public dsRuntest As DataSet

Sub Main()
    'Call FillFans()
    'Call ShowAnnouncement()


    Dim MyConString As String = "DRIVER={MySQL ODBC 3.51 Driver};" & _
                     "SERVER=quality;" & _
                     "DATABASE=runtest;" & _
                     "UID=root;" & _
                     "PASSWORD=xxxx;" & _
                     "OPTION=3"
    Dim dbcRuntest As New OdbcConnection(MyConString)
    dbcRuntest.Open()
    dsRuntest = New DataSet
    Call Fill_dsRuntest(dbcRuntest)
    Call DoAnnouncements(dbcRuntest)
End Sub

Public Function GetDataSetOdbc(ByVal sQuery As String, ByVal myConn As OdbcConnection) As DataSet
    Dim myAdapter As OdbcDataAdapter = New OdbcDataAdapter(sQuery, myConn)
    Dim myDS As New DataSet
    myAdapter.Fill(myDS)
    Return myDS
End Function

Sub WriteDataSetOdbc(ByVal sQuery As String, ByVal MyDS As DataSet, ByVal enumUpdateType As UpdateType, ByVal myConn As OdbcConnection)

    Dim myAdapter As OdbcDataAdapter
    myAdapter = New OdbcDataAdapter(sQuery, myConn)
    myAdapter.Fill(MyDS)
    Dim MyDBAdapter As OdbcDataAdapter
    MyDBAdapter = New OdbcDataAdapter(sQuery, myConn)

    Dim objAutoGen As New Odbc.OdbcCommandBuilder(MyDBAdapter)
    If enumUpdateType = UpdateType.Update Then MyDBAdapter.UpdateCommand = objAutoGen.GetUpdateCommand
    If enumUpdateType = UpdateType.Insert Then MyDBAdapter.InsertCommand = objAutoGen.GetInsertCommand
    If enumUpdateType = UpdateType.Delete Then MyDBAdapter.DeleteCommand = objAutoGen.GetDeleteCommand

    MyDBAdapter.Update(MyDS)

End Sub




Public Sub Fill_dsRuntest(ByVal dbcRuntest As OdbcConnection)
    Dim i As Integer
    Dim MyDsDatabase As DataSet
    Dim MyDsTable As DataSet
    Dim sQuery As String

    dsRuntest.Clear()
    '
    'Here, we create the tables
    '
    sQuery = "SELECT TABLE_NAME FROM information_schema.tables where table_schema='runtest'"
    MyDsDatabase = GetDataSetOdbc(sQuery, dbcRuntest)

    If MyDsDatabase.Tables(0).Rows.Count > 0 Then
        For i = 0 To MyDsDatabase.Tables(0).Rows.Count - 1
            sQuery = "SELECT * FROM " & MyDsDatabase.Tables(0).Rows(i).Item("TABLE_NAME")
            MyDsTable = GetDataSetOdbc(sQuery, dbcRuntest)
            MyDsTable.Tables(0).TableName = MyDsDatabase.Tables(0).Rows(i).Item("TABLE_NAME")
            If MyDsDatabase.Tables(0).Rows.Count > 0 Then
                dsRuntest.Tables.Add(MyDsTable.Tables(0).Copy)
            End If
        Next
    End If

End Sub

Public Sub DoAnnouncements(ByVal dbcRuntest As OdbcConnection)
    '
    'First determine what announcements need to be done
    '
    Dim drNew As DataRow
    Dim drMade() As DataRow
    Dim drNotHeard() As DataRow
    Dim sQuery As String
    sQuery = "('" & Format(Now, "yyyy-MM-dd") & "'>= TheStartDate) and ('" & Format(Now, "yyyy-MM-dd") & "'<= TheStopDate) and (TheDepartment = 'd836' or TheDepartment = 'all')"
    'MyDs = cDb.GetDataSetOdbc(sQuery, cDb.dbcRuntest)
    drMade = dsRuntest.Tables("all_announcementsmade").Select(sQuery)
    If drMade.Length > 0 Then
        Dim iCounter As Integer
        For iCounter = 0 To drMade.Length - 1
            'Then determine which ones the operator HASN'T seen
            sQuery = "idAnnouncementsMade=" & drMade(iCounter)("id") & " and " & "OperatorClockNumber='9999' and DidHear=0"
            drNotHeard = dsRuntest.Tables("all_announcementsheard").Select(sQuery)
            If drNotHeard.Length = 0 Then

                drNew = dsRuntest.Tables("all_announcementsheard").NewRow
                drNew("idAnnouncementsMade") = drMade(iCounter)("id")
                drNew("OperatorClockNumber") = "9999"
                drNew("DidHear") = 1
                dsRuntest.Tables("all_announcementsheard").Rows.Add(drNew)
                dsRuntest.AcceptChanges()
                WriteDataSetOdbc("Select * From runtest.all_announcementsheard", dsRuntest, UpdateType.Insert, dbcRuntest)


            End If
        Next
    End If

End Sub

End Module