ODBC - 无法分配环境句柄

时间:2013-12-15 16:31:10

标签: vb.net odbc navision

您好我正在尝试将数据从Navision插入DataTable数据库。 DataTable包含大约5000条记录,如果记录数量少于它的工作正常但记录数大约为5000,我就会收到此错误。

ERROR - unable to allocate an environment handle.

这是我正在使用的代码

Public Function InsertToHHTTransferLine(ByVal dtTransferLn As DataTable, ByVal hhtNumber As String) As Integer
  Dim result As Integer
  Dim cn As OdbcConnection
  Dim dtTransferLine As DataTable
  cn = New OdbcConnection(ConnStr)
  Dim SqlStr As String = ""
  Try
  cn.Open()
  dtTransferLine = dtTransferLn
  Dim DocType As String
  DocType = "Purchase"
  Dim cmd As OdbcCommand
  Dim hhtNo As String
  hhtNo = hhtNumber
  If dtTransferLine.Rows.Count > 0 Then
  For i As Integer = 0 To dtTransferLine.Rows.Count - 1
  If dtTransferLine.Rows(i)("DOC_TYPE").ToString() = "0" Then
  DocType = "Purchase"

  End If
  If dtTransferLine.Rows(i)("DOC_TYPE").ToString() = "1" Then
  DocType = "Transfer Receipt"

  End If
  If dtTransferLine.Rows(i)("DOC_TYPE").ToString() = "2" Then
  DocType = "Transfer Shipment"

  End If
  If dtTransferLine.Rows(i)("DOC_TYPE").ToString() = "3" Then
  DocType = "Stock Count"

  End If
  Try
  SqlStr = "INSERT INTO ""HHT & Navision Line""(""Document Type"",""Document No_"",""HHT No_"",""Line No_"",""Item No_"",""Document Quantity"",""Scan Quantity"",""Unit Price"",""Posted"") VALUES('" & DocType & "','" & dtTransferLine.Rows(i)("DOC_NO").ToString() & "','" & hhtNo & "','" & dtTransferLine.Rows(i)("LINE_NO").ToString() & "','" & dtTransferLine.Rows(i)("ITEM_NO").ToString() & "'," & dtTransferLine.Rows(i)("DOC_QTY").ToString() & "," & dtTransferLine.Rows(i)("SCAN_QTY").ToString() & "," & dtTransferLine.Rows(i)("UNIT_PRICE").ToString() & ",0)"
  cmd = New OdbcCommand(SqlStr, cn)
  result = cmd.ExecuteNonQuery()
  Catch ex As Exception
  If (ex.Message.IndexOf("Illegal duplicate key") <> -1) Then
  CreateLog(SqlStr, "User1", "Duplicate()", ex.Message)
  Else
  CreateLog(SqlStr, "User1", "Other()", ex.Message)
  End If
  'CreateLog(SqlStr, "User1", "Other()", ex.Message)
  End Try

  Next
  End If
  Catch ex As Exception

  CreateLog(SqlStr, "User1", "InsertToHHTTransferLine()", ex.Message)
  result = -1
  Finally
  cn.Close()
  cn.Dispose()
  End Try

  Return result
 End Function

1 个答案:

答案 0 :(得分:0)

在创建新的变量之前,是否需要处理“cmd”变量? 至少这是我在代码中可以看到的唯一一个你在循环中消耗资源的东西,具体取决于记录的数量。

无论如何,这应该很容易用调试器来识别,只需找出给出错误的行,这将引导您找到答案。