DataBase connection lost details

Hello to all. I have Next question .. 


private void timer1_Tick(object sender, System.EventArgs e)

{

sqlDataAdapter1.Fill(dataSet11);

DataRowCollection f = dataSet11.Tables["INCOMING_SMS_MESSAGES"].Rows;

for(int i = 0; i < f.Count ; i++)

{

f [ i ] .Delete();

sqlDataAdapter1.Update(dataSet11);

}


 



When timer is fired i read from data base, proccess rows and delete them. But if connection will be lost for example before sqlDataAdapter1.Update(dataSet11); programm will generate exception and terminate itsefl.(but program must avoid termination or some kind of breaks ) it must "stop" and "wait" for connection to resume

is there some standart  solution
Thank you all in advance.



Answer this question

DataBase connection lost details

  • Matthias2005

    one more how to catch momment when connection with mssql server is lost and how to terminate timer without wait for timer handle to finish
    thanks again

  • mallemukken

    The 'standard' solution is not to do what you're trying to do ;)

    Specifically, the standard for database access is to follow just in time connection and as soon as possible release. So the proper way to do what you've described would be to allow the control of the connection to be performed within this method. This method opens and closes the connection, as does the data access method in the rest of your application.

    If that is not possible within your application, then the approach you need to take is to treat the connection as a shared variable in a multi-threaded environment. This means using a lock to synchronize access to the connection object.

    Hope that helps.

  • Thomas Scheidegger

    The SqlConnection class exposes a StateChange event. This event fires when the state (such as when the connection is closed) changes.

    The termination of the timer can be done by using the Stop method.

    Since I believe I see where you are going, there is the potential for a timing issue here. It is possible that the SqlConnection closes, your timer goes off causing a data access attempt and then the StateChange event is fired. This sequence will not happen often, but when it does, you'll get the exception that you've already described. And it will be the worst kind of bug...one that appears sporadically and that can't easily be reproduced.

    Hope that helps.

  • urbs44

    1)about timer . stop method doesn't kill this


    private void timer1_Tick(object sender, System.EventArgs e)


     



    instantly. Stop method wait for timer1_tick to finish and the kill timer . this is not what i want.

    2) i need method that will say that connection lost right after it's lost. is StateChange stand for it...

    3)sorry i didn't get what are you trying to say in this paragraph:

    So the proper way to do what you've described would be to allow the control of the connection to be performed within this method. This method opens and closes the connection, as does the data access method in the rest of your application.

    which method you meen by words "this method"

    4) what about this.

    try

    {

       sqlDataAdapter1.Fill(dataSet11);

       DataRowCollection f = dataSet11.Tables["INCOMING_SMS_MESSAGES"].Rows;

       for(int i = 0; i < f.Count ; i++)

    {

       f [ i ] .Delete();

    }

    }

    catch//(System.Data.SqlClient.SqlException r)

    {

    }

    try

    {

       sqlDataAdapter1.Update(dataSet11);

    }

    catch//(System.Data.SqlClient.SqlException r)

    {

       dataSet11.Clear();

    }


     




    if i will use this structure, is it right way. i meen i can see that if any error will ocure nothing will be done with rows in database until error gone and program will not be break with error. am i right or here can be some problem.


    PS. i am not expireance in threading and locking if you can please provide some examples
    Thank you in advance.

     

  • DataBase connection lost details