c# - count(*) doesn't work when executing reader -
c# - count(*) doesn't work when executing reader -
i'm trying fetch info 1 table , count(*) another.
here's c# code:
blogcat = new list<blogcategory>(); seek { using (odbcconnection connection = new odbcconnection(configurationmanager.connectionstrings["mysqlconnstr"].connectionstring)) { connection.open(); using (odbccommand command = new odbccommand("select c.*, count(pc.id) 'total' blogcategories c bring together blogpostcat pc on pc.cid = c.id", connection)) using (odbcdatareader dr = command.executereader()) { while (dr.read()) { blogcat.add(new blogcategory((int)dr["id"], (int)dr["parent"], dr["name"].tostring(), (int)dr["total"])); } dr.close(); } connection.close(); } } grab (exception ex) { }
when run on mysql shows me result want, when execute here doesn't add together object.
select c.* , count(pc.id) 'total' blogcategories c bring together blogpostcat pc on pc.cid = c.id
how can create work?
i guess sql-string not same 1 execute in mysql.
select c.*, count(pc.id) 'total' blogcategories c bring together blogpostcat pc on pc.cid = c.id
there no grouping by, , yet utilize aggregate function count(pc.id) query other fields. syntax wrong. @ code. have try-catch around c# routine , don't log error ...
try
catch (exception ex) { console.writeline(ex.message); }
for change.
you should check connection state not open before
connection.open();
(pooling)
the same true
connection.close();
also try
system.convert.toint32(dr["your_field"])
instead of
(int)dr["your_field"]
and i'd use
system.convert.tostring(dr["name"])
instead of
dr["name"].tostring()
c# mysql asp.net
Comments
Post a Comment