vb.net - How to make SqlDataReader thread safe / convert to thread safe type -


i dealing legacy applcation written in vb.net. have been tasked use paralel task library , thread application. bulk of "work" in 1 while loop centers around sqldatareader object. credit of developer broken logical methods , 1 real work method. major problem of methods handle individual tasks accept sqldatareader parameter. know sqldatareader isn't thread safe , in way being used not thread safe @ all. want think convert sqldatareader concurrentqueue or ienumerable , have "work" thread , work on data in collection:

using command new sqlcommand(storedprocedure, connection) command.commandtype = commandtype.storedprocedure command.commandtimeout = 0 command.parameters.add("@carrier", sqldbtype.varchar, 50).value = scarrier using reader sqldatareader = command.executereader     if reader.hasrows = true         setreaderordinals(reader)         adjustments = new stringbuilder           'todo appears bulk of work in application         while reader.read             adjustments.clear()             commitcount += 1             if reader.isdbnull(somevalue) = false                 select case stuff                     case 1                         dothingforone(reader)                     case 2                         dothingfortwo(reader)                     case 3                         dothingforthree(reader)                     case 4                         dothingforfour(reader)                     case 5                         dothingforfive(reader)                     case 6                         dothingforsix(reader)                     case else                         'log                         exit while                 end select             else                 'we failed             end if         end while 

inside methods rdr acted upon in methods ex:

if rdr.getstring(somevalue).trim.length >= 5  if rrd.isdbnull(somevalue)  rrd.getint32(somevalue) 

what like:

'i know isn't how convert not sure how dim rows ienumerable(of myobject) = reader  'create threads , spawn them here  'act upon collection here in many threads parallel.for each row in rows     'do row stuff here      if row not nothing                  select case stuff                     case 1                         dothingforone(row)                     case 2                         dothingfortwo(row)                     case 3                         dothingforthree(row)                     case 4                         dothingforfour(row)                     case 5                         dothingforfive(row)                     case 6                         dothingforsix(row)                     case else                         'log                         exit                 end select             else                 'we failed             end if         end 

i not sure if plausible or best way handle this, first thing came mind.

any suggestions?

if put database reader code iterator function, give way convert ienumerable, e.g.

iterator function readmyobjects() ienumerable(of myobject)     using cn = new sqlconnection("...")         cn.open()         using cmd = new sqlcommand("...", cn)             using rdr = cmd.executereader()                 if rdr.hasrows                     while rdr.read()                         yield new myobject() {                             .propa = rdr.getstring(rdr.getordinal("a"))                         }                     end while                 end if             end using         end using     end using end function 

you can use in parallel loop:

parallel.foreach(     readmyobjects(),     sub(item myobject)         ' item     end sub ) 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -