T
The Mad Ape
Hello
I have an app that allows the user to create an access 'mdb' file
using vb.net (vs 2005). However I can not get this code to release the
database (it is locked). Please see my code below and my description
of the error message beside the offending line.
Thanks
TMA
Dim strFi As String = Application.StartupPath & "\Projects\" &
lblExport.Text & "\Export"
Dim fi As FileInfo
Dim SourceDir As String = strFi
Dim source As DirectoryInfo = New DirectoryInfo(SourceDir)
If Not source.Exists Then
source.Create()
End If
For Each fi In source.GetFiles("*.mdb")
If UCase(fi.Name) = UCase("reptree.mdb") Then
fi.Delete() ' code fails here on second time I run it
with the following error: The process cannot access the file
'reptree.mdb' because it is being used by another process. If you look
at my code below, I am closing and disposing the connection, yet the
locking file reptree.ldb remains. Why? How do I fix this?
Exit For
End If
Next
Dim catNewDB As New ADOX.Catalog
catNewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & strFi & "\reptree.mdb")
'To make tables I use Adonet
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.
4.0;Data Source=" & strFi & "\reptree.mdb;")
Dim cmdRAW As New OleDb.OleDbCommand("CREATE TABLE
tblRepTreeRAW ( " & _
"AutoId int identity ," & _
"Stand NVarchar(100)," & _
"Point NVarchar(100)," & _
"Species NVarchar(3)," & _
"Height Numeric(20,2)," & _
"Age Numeric(20,2)," & _
"DBH Numeric(20,2)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
Dim cmdSUM As New OleDb.OleDbCommand("CREATE TABLE
tblRepTreeSUM ( " & _
"AutoId int identity ," & _
"Stand NVarchar(100)," & _
"Species NVarchar(3)," & _
"AvgHeight Numeric(20,2)," & _
"AvgAge Numeric(20,2)," & _
"AvgDBH Numeric(20,2)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
conn.Open()
Try
cmdRAW.ExecuteNonQuery()
cmdSUM.ExecuteNonQuery()
'add data to tblRAW
Dim strRAW As String
Dim icountRAW As Integer
Dim strSUM As String
Dim icountSum As Integer
Dim lviRaw As ListViewItem
Dim lviSum As ListViewItem
For Each lviRaw In lvRep.Items
strRAW = "insert into tblRepTreeRAW (Stand, Point,
Species, Height, Age, DBH) values ('" & lviRaw.SubItems(5).Text &
"','" & lviRaw.Text & "','" & lviRaw.SubItems(1).Text & "', " &
CDbl(lviRaw.SubItems(2).Text) & ", " & CDbl(lviRaw.SubItems(3).Text) &
", " & CDbl(lviRaw.SubItems(4).Text) & ")"
cmdRAW = New OleDbCommand(strRAW, conn)
icountRAW = cmdRAW.ExecuteNonQuery
Next
For Each lviSum In lvStRep.Items
strSUM = "insert into tblRepTreeSUM (Stand, Species,
AvgHeight, AvgAge, AvgDBH) values ('" & lviSum.Text & "','" &
lviSum.SubItems(1).Text & "', " & CDbl(lviSum.SubItems(2).Text) & ", "
& CDbl(lviSum.SubItems(3).Text) & ", " & CDbl(lviSum.SubItems(4).Text)
& ")"
cmdSUM = New OleDbCommand(strSUM, conn)
icountSum = cmdSUM.ExecuteNonQuery
Next
cmdRAW.Dispose()
cmdRAW = Nothing
cmdSUM.Dispose()
cmdSUM = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
If Not lblBulk.Text = "BULK" Then
Beep()
MsgBox("Representative Tree GIS database created
sucessfully", MsgBoxStyle.Information)
End If
lblBulk.Text = "BULK"
Catch ex As OleDb.OleDbException
MessageBox.Show("Rep Tree Error. " & ex.Message,
"OleDbException")
cmdRAW.Dispose()
cmdRAW = Nothing
cmdSUM.Dispose()
cmdSUM = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
lblBulk.Text = "BULK"
Exit Sub
Catch ex As Exception
MessageBox.Show("Rep Tree Error. " & ex.Message,
"GeneralException")
cmdRAW.Dispose()
cmdRAW = Nothing
cmdSUM.Dispose()
cmdSUM = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
lblBulk.Text = "BULK"
Exit Sub
End Try
I have an app that allows the user to create an access 'mdb' file
using vb.net (vs 2005). However I can not get this code to release the
database (it is locked). Please see my code below and my description
of the error message beside the offending line.
Thanks
TMA
Dim strFi As String = Application.StartupPath & "\Projects\" &
lblExport.Text & "\Export"
Dim fi As FileInfo
Dim SourceDir As String = strFi
Dim source As DirectoryInfo = New DirectoryInfo(SourceDir)
If Not source.Exists Then
source.Create()
End If
For Each fi In source.GetFiles("*.mdb")
If UCase(fi.Name) = UCase("reptree.mdb") Then
fi.Delete() ' code fails here on second time I run it
with the following error: The process cannot access the file
'reptree.mdb' because it is being used by another process. If you look
at my code below, I am closing and disposing the connection, yet the
locking file reptree.ldb remains. Why? How do I fix this?
Exit For
End If
Next
Dim catNewDB As New ADOX.Catalog
catNewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data
Source=" & strFi & "\reptree.mdb")
'To make tables I use Adonet
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.
4.0;Data Source=" & strFi & "\reptree.mdb;")
Dim cmdRAW As New OleDb.OleDbCommand("CREATE TABLE
tblRepTreeRAW ( " & _
"AutoId int identity ," & _
"Stand NVarchar(100)," & _
"Point NVarchar(100)," & _
"Species NVarchar(3)," & _
"Height Numeric(20,2)," & _
"Age Numeric(20,2)," & _
"DBH Numeric(20,2)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
Dim cmdSUM As New OleDb.OleDbCommand("CREATE TABLE
tblRepTreeSUM ( " & _
"AutoId int identity ," & _
"Stand NVarchar(100)," & _
"Species NVarchar(3)," & _
"AvgHeight Numeric(20,2)," & _
"AvgAge Numeric(20,2)," & _
"AvgDBH Numeric(20,2)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
conn.Open()
Try
cmdRAW.ExecuteNonQuery()
cmdSUM.ExecuteNonQuery()
'add data to tblRAW
Dim strRAW As String
Dim icountRAW As Integer
Dim strSUM As String
Dim icountSum As Integer
Dim lviRaw As ListViewItem
Dim lviSum As ListViewItem
For Each lviRaw In lvRep.Items
strRAW = "insert into tblRepTreeRAW (Stand, Point,
Species, Height, Age, DBH) values ('" & lviRaw.SubItems(5).Text &
"','" & lviRaw.Text & "','" & lviRaw.SubItems(1).Text & "', " &
CDbl(lviRaw.SubItems(2).Text) & ", " & CDbl(lviRaw.SubItems(3).Text) &
", " & CDbl(lviRaw.SubItems(4).Text) & ")"
cmdRAW = New OleDbCommand(strRAW, conn)
icountRAW = cmdRAW.ExecuteNonQuery
Next
For Each lviSum In lvStRep.Items
strSUM = "insert into tblRepTreeSUM (Stand, Species,
AvgHeight, AvgAge, AvgDBH) values ('" & lviSum.Text & "','" &
lviSum.SubItems(1).Text & "', " & CDbl(lviSum.SubItems(2).Text) & ", "
& CDbl(lviSum.SubItems(3).Text) & ", " & CDbl(lviSum.SubItems(4).Text)
& ")"
cmdSUM = New OleDbCommand(strSUM, conn)
icountSum = cmdSUM.ExecuteNonQuery
Next
cmdRAW.Dispose()
cmdRAW = Nothing
cmdSUM.Dispose()
cmdSUM = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
If Not lblBulk.Text = "BULK" Then
Beep()
MsgBox("Representative Tree GIS database created
sucessfully", MsgBoxStyle.Information)
End If
lblBulk.Text = "BULK"
Catch ex As OleDb.OleDbException
MessageBox.Show("Rep Tree Error. " & ex.Message,
"OleDbException")
cmdRAW.Dispose()
cmdRAW = Nothing
cmdSUM.Dispose()
cmdSUM = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
lblBulk.Text = "BULK"
Exit Sub
Catch ex As Exception
MessageBox.Show("Rep Tree Error. " & ex.Message,
"GeneralException")
cmdRAW.Dispose()
cmdRAW = Nothing
cmdSUM.Dispose()
cmdSUM = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
lblBulk.Text = "BULK"
Exit Sub
End Try