Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error occurs when trying to write to database

Status
Not open for further replies.

TonyVBnet

Programmer
Mar 31, 2004
17
DE
hi.
i've got a prob with an application. it helps you to find urls in different filetypes.
i made some loops and wanted to store the data in a database. it works, but it's not complete. some files and urls are lost. do someone know why? another problem: the values for my primarykey are written as a four-figured number! it seems, as if access remembers the last item and takes this number as the first for the next items. do you know what i mean? it's a little difficult, because my english is really bad :)
following error codes appear when he tries to write in the "connection-table": "the changes could not be made (i don't know if its right, because my errormessage is in german), because the index, the primary key or the connection would contain multiple values." so it must have something to do with the loops in my opinion (perhaps its important: my database is an access-db).
here's my code, perhaps you could see, where the mistake is.
Code:
Public Sub Search()
        Dim sr As IO.StreamReader
        Dim fw As New StreamWriter(_textSearch, True)
        Dim s As String
        Dim r As Regex
        Dim searchWord As String
        Dim m As MatchCollection
        Dim o As Long = 0
        Dim myconn As New OleDbConnection _
        ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & _
        _dbName & ";")
        Try
            myconn.Open()
            Dim mycomm As New OleDbCommand("DELETE * FROM FilesLinks", _
            myconn)
            mycomm.ExecuteNonQuery()
        Catch ex As Exception
            fw.WriteLine("following error occored: " & _
            ex.Message)
        Finally
            myconn.Close()
        End Try
        Try
            myconn.Open()
            Dim mycomm As New OleDbCommand("DELETE * FROM SourceFiles", _
            myconn)
            mycomm.ExecuteNonQuery()
        Catch ex As Exception
            fw.WriteLine("following error occured: " & _
            ex.Message)
        Finally
            myconn.Close()
        End Try
        Try
            myconn.Open()
            Dim mycomm As New OleDbCommand("DELETE * FROM Links", myconn)
            mycomm.ExecuteNonQuery()
        Catch ex As Exception
            fw.WriteLine("following error occured: " & ex.Message)
        Finally
            myconn.Close()
        End Try
        For k = 0 To folderArray.Count - 1
            For q = 0 To typeArray.Count - 1
                Dim arr As New ArrayList()
                GetFiles(folderArray.Item(k), typeArray.Item(q), arr)
                Dim f As String
                For Each f In arr
                    For i = 0 To protocolArray.Count - 1
                        Try
                            sr = New IO.StreamReader(f)
                            s = sr.ReadToEnd
                            sr.Close()
                            searchWord = "(""(" & protocolArray.Item(i) _
                            & "[^""]*)"")"
                            r = New Regex(searchWord, _
                            RegexOptions.IgnoreCase Or _
                            RegexOptions.Compiled)
                            m = r.Matches(s)
                            For o = 0 To m.Count - 1

                                Dim lSource As Long
                                Dim lLinks As Long

                                Try
                                    myconn.Open()
                                    Dim mycomm As New OleDbCommand("SELECT " & _
                                    "IIF(COUNT(SourceID) " & _
                                    "= 0, 0, SourceID) FROM SourceFiles " & _
                                    "WHERE Quellpfad LIKE '" & f & "'" & _
                                    "GROUP BY SourceID", myconn)
                                    lSource = MyComm.ExecuteScalar()
                                    If lSource = 0 Then

                                        Dim mycommand As New OleDbCommand _
                                        ("INSERT INTO SourceFiles" & _
                                          "(Source, SearchDate, SearchTime)" & _
                                          "VALUES (?,?,?)", myconn)
                                        myCommand.Parameters.Add _
                                        ("@Source", OleDbType.VarChar, _
                                        250,"Source").Value = f
                                        'myComm.Parameters.Add _
                                        '("@SourceID", OleDbType.Integer, _
                                        '20, "SourceID").Value = o
                                        myCommand.Parameters.Add _
                                        ("@SearchDate", OleDbType.DBDate, _
                                        12, "SearchDatum").Value = Now.Date
                                        myCommand.Parameters.Add _
                                        ("@SearchTime", OleDbType.DBTime, _
                                        8, "SearchZeit").Value = Now.TimeOfDay
                                        myCommand.ExecuteNonQuery()
                                        Dim cmd As New OleDbCommand _
                                        ("SELECT @@identity", myconn)
                                        lSource = cmd.ExecuteScalar()
                                    Else
                                    End If
                                Catch ex As Exception
                                    fw.WriteLine(vbNewLine & "following " & _
                                    "error occured by trying to write " & _
                                    "to table SourceFiles: " & _
                                    ex.Message)
                                    fw.WriteLine()
                                Finally
                                    myconn.Close()
                                End Try

                                Try
                                    myconn.Open()
                                    Dim mycomm As New OleDbCommand _
                                    ("SELECT IIF(COUNT(LinkID) = 0, 0, LinkID)" & _
                                    " FROM Links WHERE URL LIKE '" & _
                                    m(k).Value & "' GROUP BY LinkID", myconn)
                                    lLinks = MyComm.ExecuteScalar()
                                    If lLinks = 0 Then

                                        Dim mycommand As New OleDbCommand _
                                        ("INSERT INTO Links(URL)" & _
                                        "VALUES (?)", myconn)
                                        mycommand.Parameters.Add("@URL", _
                                        OleDbType.VarChar, 250, _
                                        "URL").Value = m(k).Value
                                        mycommand.ExecuteNonQuery()
                                        Dim MyCmd As New OleDbCommand _
                                        ("SELECT @@identity", myconn)
                                        lLinks = MyCmd.ExecuteScalar()
                                    Else
                                    End If
                                Catch ex As Exception
                                    fw.WriteLine(vbNewLine & "following " & _
                                    "error occured by trying to write " & _
                                    "to table Links: " & _
                                    ex.Message)
                                    fw.WriteLine()
                                Finally
                                    myconn.Close()
                                End Try

                                Try
                                    myconn.Open()
                                    Dim mycomm As New OleDbCommand("INSERT " & _
                                    "INTO FilesLinks(LinkID, SourceID)" & _
                                    " VALUES (?, ?)", myconn)
                                    myComm.Parameters.Add("@LinkID", _
                                    OleDbType.Integer, 20, _
                                    "LinkID").Value = lLinks
                                    myComm.Parameters.Add("@SourceID", _
                                    OleDbType.Integer, 20, _
                                    "SourceID").Value = lSource
                                    myComm.ExecuteNonQuery()
                                Catch ex As Exception
                                    'this is where the error occurs                                    
                                    fw.WriteLine(vbNewLine & "following " & _
                                    "error occured by trying to write " & _
                                    "to table FilesLinks: " & ex.Message)
                                    fw.WriteLine()
                                Finally
                                    myconn.Close()
                                End Try
                                fw.WriteLine("found url: " & m(k).Value & _
                                " in: " & f)

                            Next
                            fw.WriteLine()
                            fw.WriteLine("Treffer in " & f & " für """ & _
                            protocolArray.Item(i) & """: " & m.Count)
                            fw.WriteLine()
                            s = String.Empty
                        Catch ex As Exception
                            fw.WriteLine("following error occured by " & _
                            "trying to read the file " & f & ": " _
                            & ex.Message)
                            fw.WriteLine()
                        End Try
                    Next
                Next
            Next
        Next
        fw.Close()
    End Sub
would be great, if you could help...
thx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top