Results 1 to 11 of 11

Thread: was there a better way to code this?...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    was there a better way to code this?...

    I wrote the following code to do this:

    1) read a table inside an Access .mdb file
    2) format the data to match the formatting on a SQL 2000 db
    3) write the newly formatted data into the SQL 2000 db

    there are four control not shown here: sqlConnection, sqlCommand, oleDBConnection, oleDBCommand.

    The code works but I was wondering if there was a more efficient way of doing it. maybe with datasets and the such?

    in particular: could (should) I have used Datasets? the arrays---whats a better way? I KNEW ahead of time there were just under 1000 rows. What if the row count is unknown?

    Thanks ahead everyone!!



    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         AccessConn.Open()
    3.  
    4.         Dim reader As OleDb.OleDbDataReader
    5.  
    6.         reader = AccessCMD.ExecuteReader
    7.  
    8.         Dim strTmp As String
    9.         Dim artist(1000) As String
    10.         Dim song(1000) As String
    11.         Dim mag(1000) As String
    12.         Dim month(1000) As String
    13.         Dim year(1000) As String
    14.  
    15.         Dim intCount As Integer = 0
    16.  
    17.         While reader.Read()
    18.  
    19.             artist(intCount) = reader.Item("Artists")
    20.             artist(intCount) = artist(intCount).Replace("'", "º")
    21.  
    22.             song(intCount) = reader.Item("Song")
    23.             song(intCount) = song(intCount).Replace("'"c, "º"c)
    24.  
    25.             mag(intCount) = reader.Item("Magazine")
    26.  
    27.             strTmp = reader.Item("Month/Year")
    28.  
    29.             If strTmp = "1995" Then
    30.                 strTmp = "Jan/1995"
    31.             End If
    32.  
    33.             year(intCount) = strTmp.Substring(strTmp.IndexOf("/") + 1)
    34.  
    35.             month(intCount) = strTmp.Substring(0, strTmp.IndexOf("/"))
    36.  
    37.             Select Case month(intCount)
    38.  
    39.                 Case "Jan"
    40.                     month(intCount) = "January"
    41.                 Case "Feb"
    42.                     month(intCount) = "February"
    43.                 Case "Mar"
    44.                     month(intCount) = "March"
    45.                 Case "Apr"
    46.                     month(intCount) = "April"
    47.                 Case "May"
    48.                     month(intCount) = "May"
    49.                 Case "Jun"
    50.                     month(intCount) = "June"
    51.                 Case "Jul"
    52.                     month(intCount) = "July"
    53.                 Case "Aug"
    54.                     month(intCount) = "August"
    55.                 Case "Sep"
    56.                     month(intCount) = "September"
    57.                 Case "Oct"
    58.                     month(intCount) = "October"
    59.                 Case "Nov"
    60.                     month(intCount) = "November"
    61.                 Case "Dec"
    62.                     month(intCount) = "December"
    63.  
    64.             End Select
    65.  
    66.             Select Case mag(intCount)
    67.                 Case "Guitar(fpm)"
    68.                     mag(intCount) = "Gftpm"
    69.             End Select
    70.  
    71.             intCount += 1
    72.  
    73.         End While
    74.  
    75.         reader.Close()
    76.         AccessConn.Close()
    77.  
    78.         SqlConn.Open()
    79.  
    80.         For c As Integer = 0 To intCount
    81.  
    82.             SqlCMD.CommandText = "Insert Into Tabs " & _
    83.                 "(Artist, Song, Magazine, Month, Year) Values('" & _
    84.                 artist(c) & "', '" & song(c) & "', '" & mag(c) & _
    85.                 "', '" & month(c) & "', '" & year(c) & "')"
    86.  
    87.             SqlCMD.ExecuteNonQuery()
    88.  
    89.         Next
    90.  
    91.         MessageBox.Show("Done")
    92.  
    93.     End Sub

  2. #2
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    I don't think so if you're using a Data Environment... and it seems to me that you are.

    That probably is the best way to transfer data between two database formats...

    I would personally break it down into a few different functions just to make it look a little neater, but that's just me ...

    Nice...

    ~Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I agree with squirrelly1 about breaking this method into more specific methods or properites (like artist , songs ...etc) . And you could alwasy make use of the "Imports" statements to shorten your code . Lastly , ArrayList is much more faster than the the regular array you're using .

  4. #4
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Definately right again Pirate...

    BTW... What object uses the Imports statement?

    Or method rather...

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  5. #5
    Lively Member
    Join Date
    Apr 2004
    Posts
    95
    Truthfully, I don't know. I gotta ask why you think you have to format all those values though. If you are controlling the databases on both ends, can't you make them compatible from the getgo? And just use the SqlAdapter to update the server, or fill a local dataset.

    Oh, as far as the 1000 rows thing, you don't HAVE to declare how many entries you are going to have max in your string arrays, do you? I mean, I'm honestly asking, because I don't know.
    Last edited by Kt3; May 11th, 2004 at 03:26 PM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    great suggestions!!

    the 1000 I declared was necessary because I kept being told by the compiler that I didn't have a reference to the object.(can't remember the exact error). I really don't want to hard-code 1000 in there but that's what I knew was enough. Any ideas?

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Andy
    great suggestions!!

    the 1000 I declared was necessary because I kept being told by the compiler that I didn't have a reference to the object.(can't remember the exact error). I really don't want to hard-code 1000 in there but that's what I knew was enough. Any ideas?
    Use the dynamic array object (ArrayList) or (SortedArray or something like this) . By using this object you don't have to specify number of elements you will be using .

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by squirrelly1
    Definately right again Pirate...

    BTW... What object uses the Imports statement?

    Or method rather...

    Squirrelly1
    You mean :
    Imports System.Data.OleDB

    sorry if I missed your point !

  9. #9
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    lol...
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  10. #10
    Lively Member
    Join Date
    Apr 2004
    Posts
    95
    Originally posted by Pirate
    Use the dynamic array object (ArrayList) or (SortedArray or something like this) . By using this object you don't have to specify number of elements you will be using .
    ...and when you're done, don't forget to copy it to a strongly typed array object. It's a method that you can call from the ArrayList object.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Kt3
    ...and when you're done, don't forget to copy it to a strongly typed array object. It's a method that you can call from the ArrayList object.
    You don't need to do this . You can have a property that checks the object that you want to add before you add it to the arraylist collection .

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