Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

VS 2010 Is this database update/insert code correct..it runs really slow for me

$
0
0
It just runs really slow..course i'm basing my idea of slow on a comparison to loading the same data into a listview.
does loading a database have a beginupdate/endupdate like a listview? if so maybe that's what i'm missing???
However on good note all the bugs seem to be gone now..so that's a plus :-)

Code:

    Dim LocDir As String = "C:\"
    Dim dsNewRefLibRow = New OleDb.OleDbCommand
    Dim dt As DataTable
    Dim dv As DataView
    Public WithEvents Import As BackgroundWorker
    Public ItemList As New List(Of Manual)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ds.Clear()
        Import = New BackgroundWorker
        With Import
            .WorkerReportsProgress = True
            .WorkerSupportsCancellation = True
        End With
 
              CurrentDBFiles = "RefLib.accdb"
                TempDB = "NewRefLib"
                dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
                Dim fldr As String = LocDir & CurrentDBFiles
                dbSource = "Data Source = " & fldr
                conRefLib.ConnectionString = dbProvider & dbSource
                conRefLib.Open()

                sqlRefLib = "SELECT * FROM AssistDB"
                daRefLib = New OleDb.OleDbDataAdapter(sqlRefLib, conRefLib)
                daRefLib.Fill(dsRefLib, TempDB)
                conRefLib.Close()

    End Sub

    Private Sub Import_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Import.DoWork
 
        Dim Import As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim lines() As String = File.ReadAllLines(strFileName)
        ItemList.AddRange((From l In lines Let NLine = l.Split("|"c) Select New Manual With { _
                      .StrModel = Trim(NLine(0)), _
                      .StrDocument = Trim(NLine(1)), _
                      .StrDescription = Trim(NLine(2)), _
                      .StrByteSize = Trim(NLine(3)), _
                      .StrFileSize = Trim(NLine(4)), _
                      .StrStatus = Trim(NLine(5)), _
                      .StrLocation = Trim(NLine(6)), _
                      .StrCRC32 = Trim(NLine(7)), _
                      .StrFileName = Trim(NLine(8)), _
                      .StrSite = Trim(NLine(9))}).ToArray)


        For Each udtmanual In ItemList ' Starting at here everything seems really slow to me

                CurrentDBFiles = "RefLib.accdb"
                TempDB = "NewRefLib"
                dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
                RefLibFldr = LocDir & CurrentDBFiles
                dbSource = "Data Source=" & RefLibFldr
                sqlRefLib = "SELECT * FROM AssistDB"
                Check_For_RefLib_Dups(udtmanual)

        Next
        conRefLib.Close()
    End Sub

    Private Sub Check_For_RefLib_Dups(ByVal udtmanual)
        dt = dsRefLib.Tables("NewRefLib")
        dv = dt.DefaultView
        With udtmanual

            dv.RowFilter = "FileName = '" & .strfilename & "'"
            If dv.Count > 0 Then
                'MessageBox.Show("A duplicate was found")
                RefLib_Update(.StrModel, .StrDocument, .StrDescription, .StrByteSize, .StrFileSize, .StrStatus, .StrLocation, .StrCRC32, _
                              .StrFileName, .StrSite, conRefLib)
            Else
                'MessageBox.Show("A duplicate was Not found")
                RefLib_AddItem(.StrModel, .StrDocument, .StrDescription, .StrByteSize, .StrFileSize, .StrStatus, .StrLocation, .StrCRC32, _
                              .StrFileName, .StrSite, daRefLib, dsRefLib, conRefLib)
            End If

        End With
    End Sub

    Private Sub RefLib_Update(ByVal Model, ByVal Document, ByVal description, ByVal ByteSize, ByVal FileSize, ByVal Status, _
                            ByVal Location, ByVal Crc32, ByVal FileName, ByVal StrSite, ByRef conRefLib)

        'Create the command.
        Dim UpdateSQL As String = "UPDATE(AssistDB) " & _
                                "SET Model = @Model, " & _
                                "Document = @Document, " & _
                                "Description = @Description, " & _
                                "ByteSize = @ByteSize, " & _
                                "FileSize = @FileSize, " & _
                                "Status = @Status, " & _
                                "Location = @Location, " & _
                                "Crc32 = @Crc32, " & _
                                "FileName = @FileName, " & _
                                "Site = @Site " & _
                                "WHERE  FileName = @FileName"

        Dim dsNewRefLibRow = New OleDb.OleDbCommand(UpdateSQL, conRefLib)

        dsNewRefLibRow.Parameters.AddWithValue("Model", Model)
        dsNewRefLibRow.Parameters.AddWithValue("Document", Document)
        dsNewRefLibRow.Parameters.AddWithValue("Description", description)
        dsNewRefLibRow.Parameters.AddWithValue("ByteSize", ByteSize)
        dsNewRefLibRow.Parameters.AddWithValue("FileSize", FileSize)
        dsNewRefLibRow.Parameters.AddWithValue("Status", Status)
        dsNewRefLibRow.Parameters.AddWithValue("Location", Location)
        dsNewRefLibRow.Parameters.AddWithValue("Crc32", Crc32)
        dsNewRefLibRow.Parameters.AddWithValue("FileName", FileName)
        dsNewRefLibRow.Parameters.AddWithValue("Site", StrSite)

        Try
            If conRefLib.State = ConnectionState.Closed Then conRefLib.Open()
            dsNewRefLibRow.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            ' connection.Close()
        End Try
    End Sub

    Private Sub RefLib_AddItem(ByVal Model, ByVal Document, ByVal description, ByVal ByteSize, ByVal FileSize, ByVal Status, _
                              ByVal Location, ByVal Crc32, ByVal FileName, ByVal StrSite, ByRef daRefLib, ByVal dsRefLib, ByVal ConRefLib)

        Dim insertSQL As String = "INSERT INTO AssistDB "
        insertSQL &= "(Model, Document, Description, ByteSize, FileSize, Status, Location, Crc32, FileName, Site)"
        insertSQL &= "VALUES (Model, Document, Description, ByteSize, FileSize, Status, Location, Crc, FileName, StrSite)"

        dsNewRefLibRow = New OleDb.OleDbCommand(insertSQL, ConRefLib)

        dsNewRefLibRow.Parameters.AddWithValue("Model", Model)
        dsNewRefLibRow.Parameters.AddWithValue("Document", Document)
        dsNewRefLibRow.Parameters.AddWithValue("Description", description)
        dsNewRefLibRow.Parameters.AddWithValue("ByteSize", ByteSize)
        dsNewRefLibRow.Parameters.AddWithValue("FileSize", FileSize)
        dsNewRefLibRow.Parameters.AddWithValue("Status", Status)
        dsNewRefLibRow.Parameters.AddWithValue("Location", Location)
        dsNewRefLibRow.Parameters.AddWithValue("Crc32", Crc32)
        dsNewRefLibRow.Parameters.AddWithValue("FileName", FileName)
        dsNewRefLibRow.Parameters.AddWithValue("Site", StrSite)


        Try
            If ConRefLib.State = ConnectionState.Closed Then ConRefLib.Open()
            dsNewRefLibRow.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            ' connection.Close()
        End Try

    End Sub


Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>