ADOX离开文件句柄打开

时间:2011-04-27 00:14:43

标签: .net vb.net ado adox

有没有人遇到ADOX库的问题,让文件句柄打开?

我遇到了VB6的一个奇怪问题 - > VB.Net转换。有一个函数使用ADOX查看给定数据库中的表,并重置所述表中任何列的种子值,并将auto increment属性设置为true。

我将问题追溯到“col.Properties(”Autoincrement“)的调用。值”。如果这行代码被注释掉,那么当我在函数末尾关闭ADO和ADOX连接时,文件句柄会正确终止。如果我离开该行,尽管关闭调用并强制垃圾收集,句柄仍保持打开状态。

以下是关闭对象的方法:

System.Runtime.InteropServices.Marshal.ReleaseComObject(adoConn)  
Dim connection As ADODB.Connection = cat.ActiveConnection  
If Not connection Is Nothing Then  
   connection.Close()  
End If  
cat.ActiveConnection = Nothing  
cat = Nothing  

adoConn是ADO连接,cat是ADOX.Catalog对象。 col(from before)是与目录对象一起使用的ADOX.Column对象。

我最初尝试将此算法转换为使用ADO.Net,并且使用DataSet对象我接近但无法弄清楚如何确定表列是否设置为自动增量。这是使用Access 2000数据库btw。

1 个答案:

答案 0 :(得分:1)

我刚刚遇到使用ADOX创建数据库后遗留锁定文件的问题。确保连接已关闭且锁文件未被任何程序资源使用可能会有所帮助:

System.Runtime.InteropServices.Marshal.ReleaseComObject(adoConn)

Dim connection As ADODB.Connection = cat.ActiveConnection

If Not connection Is Nothing Then
  connection.Close()   
End If   

'Try adding this below
System.Runtime.InteropServices.Marshal.ReleaseComObject(connection)
System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)
GC.Collect()

cat.ActiveConnection = Nothing   
cat = Nothing