sql server ExecuteReader requires an open and available Connection. The connection's current state is closed -
i using multithreaded application extract data database , after few minutes of running start exception or sql timeout exception. code below:
public static list<flightinfo> flightinfoallpricesperdayforroute(string from, string to) { // create object array datareader list<flightinfo> arr = new list<flightinfo>(); using (var conn = new sqlconnection(sconnectionstring)) { using (var command = new sqlcommand("flightinfoallpricesperdayforroute", conn) { commandtype = commandtype.storedprocedure }) { conn.open(); command.commandtimeout = 150; command.parameters.addwithvalue("airportfrom", from); command.parameters.addwithvalue("airportto", to); // command.parameters.addwithvalue("date", dt); //execute stored procedure using (idatareader dr = command.executereader()) { flightinfo obj; while (dr.read()) { obj = new flightinfo(); obj.flightinfoid = (int)dr["flightinfoid"]; obj.airportfrom = (string)dr["airportfrom"]; obj.airportto = (string)dr["airportto"]; obj.timedeparture = datetime.parse(dr["timedeparture"].tostring()); obj.timearrival = (datetime)dr["timearrival"]; obj.price = (short)int16.parse(dr["price"].tostring()); obj.dateadded = (datetime)dr["dateadded"]; obj.carrier = (string)dr["carrier"]; arr.add(obj); } dr.close(); dr.dispose(); conn.close(); conn.dispose(); command.dispose(); } } } return arr; }
i have added teh following connection string improve timeout problem still getting errors:
min pool size=200;max pool size=32767;asynchronous processing=true; connect timeout=150;
the stored procedure simple don't think there problem there:
select *, dateadd(dd, 0, datediff(dd, 0, timedeparture)) flightdate flightinfo airportfrom = @airportfrom , airportto = @airportto , timedeparture > getdate() -- future flights order flightdate asc, price asc
i've tried running check sql connections 'the user not have permission perform action.' error
select * sys.dm_exec_connections
any suggestions appreciated!
your code looks ok - suggest on database server problem. if stored proc takes long time run and/or causes lot of locking in tables, these problems (especially if called frequently). query optimization in order.
fyi - don't need 'using' block around sqlcommand object. also, there no need call 'close' , 'dispose' on reader , connection objects - 'using' handle you.
Comments
Post a Comment