【发布时间】:2019-05-20 15:13:07
【问题描述】:
VB2013:我收到异常 System.Data.OleDb.OleDbException (0x80004005):超出系统资源。当我查询 MS Access 数据库时。
这是我在代码中所做的:
'Make the connection to connDB
Public connDB As OleDbConnection
connDB = New OleDbConnection
With connDB
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DbFile & ";Persist Security Info=True;Jet OLEDB:Database Password=xxxxxx;"
.Open()
End With
'iterate through some 2500 obects. each object has a set of codes and we will get their description here
GetSysDefinitions (list of codes for an object)
'Close the connection to connDB;
Public Function GetSysDefinitions(sysCodes As List(Of String)) As String
Try
'check to see if the db is available
If connDB Is Nothing Then Return ""
'set up the SQL to get the System Codes and Definitions
Dim sCodes As String = "'" & String.Join("', '", sysCodes) & "'"
Dim sql As String = "SELECT * " & _
"FROM SYS_Codes " & _
"WHERE CODE IN(" & sCodes & ") ; "
Dim daLs As New OleDbDataAdapter(sql, connDB)
Dim dsLs As New DataSet
Dim dtLs As New DataTable
daLs.SelectCommand.CommandTimeout = 60 '60 seconds for the command to timeout
daLs.Fill(dsLs, "Sys") '<=== Exception here at some point
dtLs = dsLs.Tables(0)
'do something with records returned
Catch ex As Exception
Debug.Print(ex.ToString)
End Try
End Function
这一切都很好。然而,在某些时候,我得到了异常 System.Data.OleDb.OleDbException (0x80004005):在 daLs.Fill 行超出系统资源。我只是不确定为什么会超出资源以及我需要做些什么来避免这种异常。好像我建立了一个连接,然后用它来进行许多查询。应该可以正常工作吗?
【问题讨论】:
-
Seems like I make one connection and then use it to make many queries,不要这样做。在需要时使用它们,并在完成后丢弃它们。恕我直言,请查看Using语句和参数...另外,您正在填写DataSet,但只使用一个表,不要那样做,只需Fill一个DataTable... -
Çöđěxěŕ 所说的以及创建 sCodes 的更简洁的方法是
Dim sCodes As String = String.Join(", ", sysCodes.Select(Function(s) "'" & s & "'"))。
标签: sql vb.net exception resources oledb