vb.net - How to avoid filling a dataset if just testing for more than 0 records existing? -
this function beingness called in cases hundreds of times in existing code. existing code costly not funny. need fill dataset? wouldn't improve maybe executenonquery , test homecoming value?
is ther improve way together?
private function getsomeinfo(byval primaryid integer) boolean dim val boolean = false dim cmdreturn sqlcommand = new sqlcommand cmdreturn.commandtext = "uspthesproc" cmdreturn.commandtype = commandtype.storedprocedure cmdreturn.parameters.add(new sqlparameter("@primaryid", primaryid)) dim dasql sqldataadapter = new sqldataadapter dasql.selectcommand = cmdreturn dim dsthedata dataset = new dataset dim connmain sqlconnection = new sqlconnection(configurationmanager.connectionstrings("mainconnection").connectionstring) seek cmdreturn.connection = connmain cmdreturn.commandtimeout = 2000 connmain.open() dasql.fill(dsthedata, "data") if (dsthedata.tables(0).rows.count > 0) val = true else val = false end if grab sqlexcept sqlexception dim lerror main.errorhandlers.erroratserver = new main.errorhandlers.erroratserver() lerror.processerror(sqlexcept, "..." + ".getcontents") homecoming val grab except exception dim lerror main.errorhandlers.erroratserver = new main.errorhandlers.erroratserver() lerror.processerror(except, "..." + ".getcontents") homecoming val if connmain.state = connectionstate.open connmain.close() end if end seek homecoming val end function
use forward-only sqldatareader , check hasrows
property.
using reader sqldatareader = cmdreturn.executereader() val = reader.hasrows end using
another way execute command scalar.
"the first column of first row in result set, or null reference (nothing in visual basic) if result set empty. returns maximum of 2033 characters." msdn
so if first column in result set int
, can this:
val = (typeof cmdreturn.executescalar() int32)
vb.net dataset
No comments:
Post a Comment