sql - how to get bool value from List<object> in c#? -
hi converting vb project c# youtube tutorial mkaatr use variable
private dbmsresultsets list(of object)
so use variable in c# private list<object> dbmsresultsets;
and later in code use function return type bool , use method
return dbmsresultsets(i).read
so use same thing visual studio give me error hover on vb code (dbmsresultsets(i).read)
it "get or set element @ specified index"
so around , find out if write return dbmsresultsets[(int)i]);
same thing ("get or set element @ specified index")
now visual studio give me error can not convert object bool use convert.to , try (bool) mean try typecast both method didn't work need giving whole vb code , conversion c# code
the problem in function readandnoteof
vb code
imports system.data.sqlclient ' class used manage connectivity database public class dbmsclass ' define connection string private dbmsconnectionstring = "data source=.\sqlexpress;attachdbfilename=c:\users\asus\desktop\librarymanagementsystem\database\lms.mdf;integrated security=true;user instance=true;multipleactiveresultsets=true" ' define connection private dbmsconnectionobj system.data.sqlclient.sqlconnection ' define transaction private dbmstransactionobj system.data.sqlclient.sqltransaction ' define commands object , result sets private dbmscommands list(of system.data.sqlclient.sqlcommand) private dbmscommandcodes list(of long) private dbmsresultsets list(of object) ' command counter private dbmscommandcounter long ' open database connection public function opendb() string try ' open connection dbmsconnectionobj = new sqlconnection(my.settings.myconnection) dbmsconnectionobj.open() ' create transaction dbmstransactionobj = dbmsconnectionobj.begintransaction ' prepare commands list dbmscommands = new list(of system.data.sqlclient.sqlcommand) dbmscommandcodes = new list(of long) dbmsresultsets = new list(of object) ' prepare command counter dbmscommandcounter = 0 ' return ok return "ok" catch ex exception return ex.message end try end function ' used run sql commands public sub executesql(byval sql string, byval paramarray obj() object) ' build command object dim cmd new system.data.sqlclient.sqlcommand(sql, me.dbmsconnectionobj, me.dbmstransactionobj) ' add parameters sql command dim integer = 0 obj.length - 1 cmd.parameters.addwithvalue("@" & i, obj(i)) next ' run sql cmd.executenonquery() end sub ' function used commit transaction public sub commit() me.dbmstransactionobj.commit() me.dbmstransactionobj = me.dbmsconnectionobj.begintransaction end sub ' function used rollback transaction public sub rollback() me.dbmstransactionobj.rollback() me.dbmstransactionobj = me.dbmsconnectionobj.begintransaction end sub ' function used create result set public function createresultset(byval sql string, byval paramarray obj() object) long dbmscommandcounter += 1 ' build command object dim cmd new system.data.sqlclient.sqlcommand(sql, me.dbmsconnectionobj, me.dbmstransactionobj) ' add parameters sql command dim integer = 0 obj.length - 1 cmd.parameters.addwithvalue("@" & i, obj(i)) next ' read data dim rs = cmd.executereader(commandbehavior.default) ' store objects in list me.dbmscommandcodes.add(dbmscommandcounter) me.dbmscommands.add(cmd) me.dbmsresultsets.add(rs) return dbmscommandcounter end function ' function used close result set public sub closeresultset(byval nmbr long) dim integer = 0 me.dbmscommandcodes.count - 1 ' find command , result set if dbmscommandcodes(i) = nmbr ' objects dim r = me.dbmsresultsets(i) dim c = me.dbmscommands(i) ' remove objects list me.dbmsresultsets.removeat(i) me.dbmscommands.removeat(i) me.dbmscommandcodes.removeat(i) ' return resources r.close() r.dispose() c.dispose() return end if next throw new exception("the command or result set not exist") end sub ' function used read single record db public function readandnoteof(byval code long) boolean ' search dim long = 0 me.dbmscommandcodes.count - 1 if dbmscommandcodes(i) = code return dbmsresultsets(i).read end if next throw new exception("command or resultset not exist") end function ' function used column value db public function getcolumnvalue(byval code long, byval columnname string) object dim long = 0 me.dbmscommands.count - 1 if dbmscommandcodes(i) = code return dbmsresultsets(i).item(columnname) end if next throw new exception("command or resultset not exist") end function end class
my c# code
//this class used manage connectivity database using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.data.sql; using system.data; using system.data.sqlclient; namespace library_main { public class dbmsclass { //define connection string // private string dbmsconnectionstring = "data source=(localdb)\\v11.0;attachdbfilename="\\d:\\tutorial\\c # tutorial\\3 may 2015\\library_main\\library_main\\bin\\debug\\database\\lms.mdf";"integrated security=true;connect timeout=30"; //define connection private sqlconnection dbmsconnectionobj = null; //define transaction private sqltransaction dbmstransactionobj; // define commands object , result sets private list<sqlcommand> dbmscommands; private list<long> dbmscommandcodes; private list<object> dbmsresultsets; // command counter private long dbmscommandcounter; //open database connection public string opendb() { try { //open connection dbmsconnectionobj = new sqlconnection(properties.settings.default.connectionstring); dbmsconnectionobj.open(); //creat transaction dbmstransactionobj = dbmsconnectionobj.begintransaction(); //prepare commands list dbmscommands = new list<sqlcommand>(); dbmscommandcodes = new list<long>(); dbmsresultsets = new list<object>(); // prepare command counter dbmscommandcounter = 0; //return ok return "ok"; } catch (exception ex) { return ex.message; } } //this used run sql commands public void exceutesql(string sql, params object[] obj) { //build command object sqlcommand cmd = new sqlcommand(sql, this.dbmsconnectionobj, this.dbmstransactionobj); //add parameters sql command int i; int count = obj.length - 1; (i = 0; <= count; i++) { cmd.parameters.addwithvalue("@" + i, obj[i]); } //run sql cmd.executenonquery(); } //this funtion commit public void commit() { this.dbmstransactionobj.commit(); this.dbmstransactionobj = this.dbmsconnectionobj.begintransaction(); } // function used rollback transaction public void rollback() { this.dbmstransactionobj.rollback(); this.dbmstransactionobj = this.dbmsconnectionobj.begintransaction(); } //this function used creat result set public long creatresultset(string sql, params object[] obj) { dbmscommandcounter += 1; // build command object sqlcommand cmd = new sqlcommand(sql, this.dbmsconnectionobj, this.dbmstransactionobj); // add parameters sql command int = 0; (i = 0; <= obj.length - 1; i++) { cmd.parameters.addwithvalue("@" + i, obj[i]); } // read data dynamic rs = cmd.executereader(system.data.commandbehavior.default); // store objects in list this.dbmscommandcodes.add(dbmscommandcounter); this.dbmscommands.add(cmd); this.dbmsresultsets.add(rs); return dbmscommandcounter; } // function used close result set public void closeresultset(long nmbr) { int = 0; (i = 0; <= this.dbmscommandcodes.count - 1; i++) { // find command , result set if (dbmscommandcodes[i] == nmbr) { // objects dynamic r = this.dbmsresultsets[i]; dynamic c = this.dbmscommands[i]; // remove objects list this.dbmsresultsets.removeat(i); this.dbmscommands.removeat(i); this.dbmscommandcodes.removeat(i); // return resources r.close(); r.dispose(); c.dispose(); return; } } throw new exception("the command or result set not exist"); } // function used read single record db public bool readandnoteof(long code) { // search long = 0; (i = 0; <= this.dbmscommandcodes.count - 1; i++) { if (dbmscommandcodes[(int)i] == code) { return convert.toboolean(dbmsresultsets[(int)i]); } } throw new exception("command or resultset not exist"); } // function used column value db public object getcolumnvalue(long code, string columnname) { long = 0; (i = 0; <= this.dbmscommandcodes.count - 1; i++) if (dbmscommandcodes[(int)i] == code) { return dbmsresultsets[(int)i].equals(columnname); } throw new exception("command or resultset not exist"); } } }
the result of calling executereader on sqlcommand object sqldatareader
see here
in part of code
// read data dynamic rs = cmd.executereader(system.data.commandbehavior.default); // store objects in list this.dbmscommandcodes.add(dbmscommandcounter); this.dbmscommands.add(cmd); this.dbmsresultsets.add(rs);
the type of rs not exact equivalent of resultset
like accrostic suggests sqldatareader
which has boolean method call read
and return true until there no more datas fetch stream
so here in boolean method called readandnoteof
instead of
return convert.toboolean(dbmsresultsets[(int)i]);
do
sqldatareader reader = (sqldatareader)dbmsresultsets[(int)i]; return reader.read();
on same pattern can column value in method getcolumnvalue if change:
return dbmsresultsets[(int)i].equals(columnname);
by
sqldatareader reader = (sqldatareader)dbmsresultsets[(int)i]; object columnvalue = reader[columnname]; return columnvalue;
see here on msdn definition of accessor this
(brackets reader[string_value]) takes string parameter (the column name)
of course need sure read method of sqldatareader has returned true (your above method)
hope helps you.
Comments
Post a Comment