检查文件是否打开,如果打开,则将错误写入访问数据库

时间:2018-10-18 17:05:01

标签: vb.net

我编写了一种方法来检查文件是否已打开,以及是否已打开将错误写入访问数据库。我打开了文件,它读取try,但从不将catch作为异常,因此将其写入数据库。回到我创建的复制方法,然后转到catch异常,在那里看到文件已打开。

  Private Sub IsFileOpen(nFile As String)
        Dim stream As FileStream
        Dim sqlError As New OleDb.OleDbCommand
        sqlError = conn.CreateCommand

        Dim docName As String = System.IO.Path.GetFileNameWithoutExtension(nFile)
        Dim fileName As String = System.IO.Path.GetFileName(nFile)
        Dim processDate As String = Date.Now
        Dim errorNum As String = ""

        Try
            stream = File.Open(nFile, FileMode.Open, FileAccess.Read)
            stream.Close()
        Catch ex As Exception
            If TypeOf ex Is Exception Then
                errorNum = ex.Message
                sqlError.CommandText = "INSERT INTO tblDocumentCopyError ([DocName], [FileName], [ProcessDate], [ErrorNumber]) " _
                                    & " VALUES (@DocName, @FileName, @ProcessDate, @ErrorNumber) "

                sqlError.Parameters.AddWithValue("@DocName", docName)
                sqlError.Parameters.AddWithValue("@FileName", fileName)
                sqlError.Parameters.AddWithValue("@ProcessDate", processDate)
                sqlError.Parameters.AddWithValue("@ErrorNumber", errorNum)
                sqlError.ExecuteNonQuery()
            Else
                MessageBox.Show(vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try

    End Sub

0 个答案:

没有答案