我在.net 4.5.2上运行了一个vb.net wpf应用程序。 此应用程序通过读写功能连接到Access 2013数据库(.accdb)(一种自定义构建的实体框架,在开始时读取,最后写入/更新/删除)。
我为每个表连续多次读取和多次写入。他们都在不说数据库被锁定的情况下工作。
我用这个函数创建了我的connectionstring:
Function newconnectionstring() As System.Data.OleDb.OleDbConnection
newconnectionstring = New System.Data.OleDb.OleDbConnection("myconnectionstring")
End Function
这是我最后的读写功能:
READ(似乎没有锁定数据库)
Sub ImportTechnicalLabels()
TechnicalLabels = New Dictionary(Of String, TechnicalLabel)(StringComparer.OrdinalIgnoreCase)
Dim accessconnection As System.Data.OleDb.OleDbConnection
accessconnection = newconnectionstring()
accessconnection.Open()
Dim sSQL As String = "SELECT * FROM TechnicalLabels"
Dim objCmd As New OleDbCommand(sSQL, accessconnection)
Dim reader As OleDbDataReader
Dim readerTechnicalLabel As TechnicalLabel
reader = objCmd.ExecuteReader
While reader.Read
readerTechnicalLabel = New TechnicalLabel
With readerTechnicalLabel
.UpdateStatus = 0
.TechnicallabelID = reader.Item("TechnicallabelID").ToString
End With
TechnicalLabels.Add(readerTechnicalLabel.TechnicalLabelID, readerTechnicalLabel)
End While
reader.Close()
accessconnection.Close()
accessconnection = Nothing
reader = Nothing
End Sub
WRITE(保持数据库锁定但允许其他表上的写入继续)
Sub UpdateConcessionlines()
Dim accessconnection As System.Data.OleDb.OleDbConnection
accessconnection = newconnectionstring()
accessconnection.Open()
Dim updatesql As String = "UPDATE Concessionlines SET ConcessionLineID=@ConlID, ConcessionID=@ConID, Defect = @Def, MeasureID=@MeID, Comment =@Com, Numberitembtd = @Nbibt, LineTechnicalDecisionID = @LTechID WHERE (ConcessionLineID = @ConlID);"
Dim updatecmd As OleDbCommand
Dim addsql As String = "INSERT INTO Concessionlines VALUES (@ConlID, @ConID, @Def, @MeID, @Com, @Nbibt, @LTechID);"
Dim addcmd As OleDbCommand
Dim deletesql As String = "DELETE FROM Concessionlines WHERE ConcessionLineID = @ConlID"
Dim deletecmd As OleDbCommand
For Each item In (From Conlis In Concessionlines.Values Where Conlis.UpdateStatus = 1 Select Conlis).ToList
updatecmd = New OleDbCommand(updatesql, accessconnection)
With updatecmd
If Not item.ConcessionLineID = Nothing Then
.Parameters.AddWithValue("@ConlID", item.ConcessionLineID)
Else
.Parameters.AddWithValue("@ConlID", DBNull.Value)
End If
'... and so on
End With
updatecmd.ExecuteNonQuery()
Next item
For Each item In (From Conlis In Concessionlines.Values Where Conlis.UpdateStatus = 2 Select Conlis).ToList
addcmd = New OleDbCommand(addsql, accessconnection)
With addcmd
If Not item.ConcessionLineID = Nothing Then
.Parameters.AddWithValue("@ConlID", item.ConcessionLineID)
Else
.Parameters.AddWithValue("@ConlID", DBNull.Value)
End If
'... and so on
End With
addcmd.ExecuteNonQuery()
Next item
For Each item In (From Conlis In Concessionlines.Values Where Conlis.UpdateStatus = 3 Select Conlis).ToList
deletecmd = New OleDbCommand(deletesql, accessconnection)
With deletecmd
If Not item.ConcessionLineID = Nothing Then
.Parameters.AddWithValue("@ConlID", item.ConcessionLineID)
Else
.Parameters.AddWithValue("@ConlID", DBNull.Value)
End If
End With
deletecmd.ExecuteNonQuery()
Next item
accessconnection.Close()
accessconnection = Nothing
End Sub
但最后,数据库被锁定,我无法再次启动整个过程。 如果我尝试手动删除accdbl文件,我会收到以下消息:
由于此文件已打开,因此无法完成此操作 WpfApplication1。
我必须完全关闭我的应用程序才能重新开始。
知道为什么我的访问数据库保持锁定状态? 或者也许任何想法在不知道是什么导致锁定的情况下释放数据库? 谢谢你的帮助。
更新
我把问题缩小了。 显然问题来自删除操作。 我现在已经以不同的方式组织了我的代码,所以我还没有解决这个问题。但我想的是问题可能来自我为某些访问关系选择的级联删除选项。待验证......