VB.NET Executereader out of memory when switch to Chinese language

63 Views Asked by At

I have this code that works very well in different languages but when I change the regional setting number format to "Chinese Traditional" Out of memory exception happens on Executereader! What to do? Thank you!

Dim dv As DataView
Dim dt As DataTable = New DataTable
dv = New DataView(dt)
Dim dr As DataRow
Dim myreader As OleDbDataReader
dbobj = New dboperation("2")
dbobj.objcmd.CommandType = CommandType.Text
dbobj.objcmd.CommandText = "SELECT [geonameid], [name], [asciiname], [alternatenames], [latitude], [longitude], [timezone], [admin1 code], [code] , [Field2], [Field3] FROM atlas_view a left join Admin1CodesASCII d on a.code=d.Field1 WHERE (@Country Is NULL OR [country code]=@Country) and (@City Is NULL OR [alternatenames] like '%'+@City+'%')" ' OR [Alt] like '%'+@City+'%')"

dbobj.objcmd.Parameters.AddWithValue("@Country", IIf(Country = "" Or Country Is Nothing, DBNull.Value, mMdl.FixString(Country)))
dbobj.objcmd.Parameters.AddWithValue("@City", IIf(City = "" Or City Is Nothing, DBNull.Value, City))

If mMdl.SQlInjection(dbobj) Then
    Return Nothing
End If

If dbobj.objcmd.Connection.State = ConnectionState.Closed Then
    dbobj.objcmd.Connection.Open()
End If

myreader = dbobj.objcmd.ExecuteReader                  
1

There are 1 best solutions below

2
Joel Coehoorn On

This isn't a direct answer, but I wanted to address several concerns in the original code and needed more space than would fit in a comment. Some of the changes are stylistic, but a few of these changes fix actual serious bugs in the original code. It also amazes me how often just fixing these kinds of things to use better patterns will fix the original issue in a question.

Dim dt As New DataTable()
Dim sql As String = "
    SELECT [geonameid], [name], [asciiname], [alternatenames], [latitude], [longitude], [timezone], [admin1 code], [code] , [Field2], [Field3] 
    FROM atlas_view a 
    LEFT JOIN Admin1CodesASCII d on a.code=d.Field1 
    WHERE 1=1"

If Not String.IsNullOrEmpty(Country) Then
     sql += " AND [country code]= @Country"
End If
If Not String.IsNullOrEmpty(City) Then
    sql += " AND ([alternatenames] like '%'+@City+'%' OR [Alt] like '%'+@City+'%')"
End If

' If the dboperation type manages your connections, it should implement IDisposable!
' Also, the official MS style guide now explicitly recommends NOT to use "obj" prefixes. That behavior died with vb6, more than 15 years ago.
Using db As New dboperation("2")

    If Not String.IsNullOrEmpty(Country) Then      
        ' Don't use AddWithValue! It can cause HUGE performance issues.        
        ' I had to guess at parameter type and length. You should use actual types and length from the database
        db.cmd.Parameters.Add("@Country", OleDbType.VarWChar, 3).Value = mMdl.FixString(Country)
    End IF
    If Not String.IsNullOrEmpty(City) Then
        db.cmd.Parameters.Add("@City", OleDbType.VarWChar, 30).Value = City
    End If

    db.cmd.Connection.Open()
    Using rdr As OleDbDataReader = db.cmd.ExecuteReader()
       dt.Load(rdr)
    End Using
End Using
Dim result As New DataView(dt)