c# - SqlCommand state closed when SqlConnection state is open -


i having intermittent issue sqldatarreader class have opened sqlconnection , state open, when create sqlcommand sqlconnection, sqlconnection's state closed. occurs approximately 1 in 10 attempts, , might timing issue.

note rather putting connection in using block, open/close connection independently execute multiple commands @ once, issue occurs on first time command executed on connection has been opened.

the connection code is:

    private sqlconnection sql;      public result connect(string database)     {         string connection = config.environments[config.environment][database];         try         {             // create , open connection             sql = new sqlconnection(connection);             sql.open();              if (sql == null || sql.state != system.data.connectionstate.open)                 return new result(false, "connect database", "could not connect database [" + connection + "]");             return new result(true, "connect database", "connected database [" + connection + "]");         }         catch (exception e)         {             return new result(false, "connect database", "could not connect database [" + connection + "] " + e.tostring());         }     } 

the run command code is:

    private datatable runsql(string statement)     {         if (sql == null || sql.state != system.data.connectionstate.open)             throw new scriptexception("cannot execute sql command, no database connection established [" + statement + "]");          // create , execute sql statement         using (sqlcommand command = new sqlcommand(statement, sql))         {             command.commandtimeout = config.sqltimeout;              try             {                 using (sqldatareader reader = command.executereader()) // error occurs here! - sql.state open, command.state closed ???                 {                     // check reader has rresults                     if (reader.hasrows)                     {                         datatable data = new datatable();                         data.load(reader);                         return data;                     }                     else                     {                         throw new exception("no results found statement: " + statement + ", on server: " + sql.datasource + ", in database: " + sql.database);                     }                 }             }             catch (sqlexception)             {                 //log things here             }              throw new scriptexception("error executing sql command: " + statement);         }     } 

the code reproduces issue (occasionally):

    private datatable runsinglecommand(string database, string command)     {         log(connect(database));         return runsql(command);     } 

what can suggest inside connection command before opening connection , give condition check know whether connection open or not. if in closed mode open new connection

try {     // create , open connection     sql = new sqlconnection(connection);      if (sql.state != system.data.connectionstate.open)     {           sql.open();     }      return new result(true, "connect database", "connected database [" + connection + "]");  }     catch (exception e)  {     return new result(false, "connect database", "could not connect database [" + connection + "] " + e.tostring()); } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -