Hi all,
I have one question pending in my mind and mayb you could help me to understand what would be if I create some commands for database manipulation with try/catch clause. By Creating new "Particijos" instance I instantiate Objects for holding all the database commands:
public Particijos (Particija particija) { //Constructor
switch(particija) {
case Particija.LRP:
//some fields
_accessKomandos = new AccessKomandos(...);
break;
case Particija.LRS:
//Some Fields
_accessKomandos = new AccessKomandos(...);
break;
case Particija.LRV:
//Some Fields
_accessKomandos = new AccessKomandos(...);
break;
default:
//Some Fields
_accessKomandos = new AccessKomandos(...);
break;
}
}
In each _accessKomandos Instance I create instances of database Commands:
public class AccessKomandos{
private readonly static OleDbConnection OleConn = new OleDbConnection(ConfigurationManager.AppSettings.Get("ConnectionString"));
private readonly OleDbCommand _oleCmdTableAppendQuery;
private readonly OleDbCommand _oleCmdDeleteAllFromUpdateTable;
public AccessKomandos (...) {
_oleCmdTableAppendQuery = SudarymasOleCmdQuery(...);
_oleCmdDeleteAllFromUpdateTable = SudarymasDeleteAllFromTable(...);
}
private static OleDbCommand SudarymasOleCmdQuery (string inputQueryTableVardas) {
OleDbCommand oleCmdQuery = new OleDbCommand();
oleCmdQuery.Connection = OleConn;
oleCmdQuery.CommandType = CommandType.StoredProcedure;
oleCmdQuery.CommandText = inputQueryTableVardas;
oleCmdQuery.Prepare();
return oleCmdQuery;
}
private static OleDbCommand SudarymasDeleteAllFromTable (...) {
OleDbCommand oleCmdDelete = new OleDbCommand("DELETE * FROM " + lentelesVardas, OleConn);
oleCmdDelete.Prepare();
return oleCmdDelete;
}
public void TrintiUpdateLentele () {
_oleCmdDeleteAllFromUpdateTable.ExecuteNonQuery();
}
public void VykdytiAppendQuery () {
_oleCmdTableAppendQuery.ExecuteNonQuery();
}
So by creating Instance of AccessKomandos I create instances of Commands in this case _oleCmdTableAppendQuery and _oleCmdDeleteAllFromUpdateTable, prepare them and then I can call them whenever I want in the next code (main program)
while(count<4){
try{
AccessKomandos.OpenConnection();
particijaA = new Particijos(Particija.A);
particijaB = new Particijos(Particija.B);
particijaC = new Particijos(Particija.C);
particijaA.AccessKomandos.TrintiUpdateLentele ();
particijaB.AccessKomandos.TrintiUpdateLentele ();
particijaC.AccessKomandos.TrintiUpdateLentele ();
particijaA.AccessKomandos.VykdytiAppendQuery();
particijaB.AccessKomandos.VykdytiAppendQuery();
particijaC.AccessKomandos.VykdytiAppendQuery();
Accessomandos.CloseConnection();
}
catch (Exception){
//logging...
//waiting for 60sec
count++;
}
}
The question would be: If something wrong happens at some point in try{} statement and I try to do everything again (because of while(count<4)), should I Dispose all the instantiated commands in catch{} block ? Lets pretend that I cannot move instantiation of Particija out of try{} block because it is nested and I have given here only simplified version. Or should I expect that GC would dispose those commands by itself, as I understand after jumping from try{} block to catch{} block, all in try{} instantiated objects will be out of scope and appears for GC to collect ?
Thanks for your answers